之前做的邮件发送系统中就遇到了这个问题,用程序清空txt然后写入内容。
FileStream stream2 = File.Open(inforpath, FileMode.OpenOrCreate, FileAccess.Write);
stream2.Seek(0, SeekOrigin.Begin);
stream2.SetLength(0); //清空txt文件
stream2.Close();
File.WriteAllLines(PathBase + inforpath, content1);
这个问题找了很久才找到。网上说的用stream打开然后关闭、写入空字符等等,我都试了,但不行。
这个小程序是之前做的,今天把它晾上来了。程序比较简单,,适合初学者学习。先给个截图:

实现单发和多发功能,可附加多份附件,限制总大小为1M,大于后无法发送。
默认发送格式为Html。
【修改】【添加】的操作是针对发件人的,点击【修改】弹出下图界面:

填充文本框的是在ThreadSendEmail.cs中的构造函数中
/// <summary>
/// 发送邮件
/// </summary>
/// <param name="strSenderEmail">发件人邮箱</param>
/// <param name="strTitle">邮件标题</param>
/// <param name="strBody">邮件正文</param>
/// <param name="ToEmail">收件人邮箱</param>
/// <param name="AttachmentFiles">附件</param>
public ThreadSendEmail(string strSenderEmail, string strTitle, string strBody, string ToEmail, ArrayList AttachmentFiles)
{
this.strSenderEmail = strSenderEmail;
strToEmail = ToEmail;
strEmailTitle = strTitle;
strEmailBody = strBody;
arrAttachmentFiles = AttachmentFiles;
using (FileStream stream = new FileStream(inforpath, FileMode.OpenOrCreate, FileAccess.Read))
{
using (StreamReader read = new StreamReader(stream, Encoding.Default))
{
string GetSenderInfor;
while ((GetSenderInfor = read.ReadLine()) != null)
{
string[] line = GetSenderInfor.Trim().Split(' ');
if (line[1] == strSenderEmail)
{
strSenderSMTP = line[0];
strSnederPwd = line[2];
}
}
}
}
}
当然,点击【添加】文本框中是没有内容的。
下面的函数是发送邮件函数
/// <summary>
/// 发送邮件
/// </summary>
/// <returns>true:发送成功 false:发送失败</returns>
public bool Send()
{
SmtpClient client = new SmtpClient();
client.Host = strSenderSMTP;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(strSenderEmail, strSnederPwd);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
MailMessage mm = new MailMessage(strSenderEmail, strToEmail, strEmailTitle, strEmailBody);
mm.BodyEncoding = Encoding.Default;
mm.IsBodyHtml = true;
if (arrAttachmentFiles.Count != 0)
{
foreach (string attach in arrAttachmentFiles)
{
Attachment attachment = new Attachment(attach, System.Net.Mime.MediaTypeNames.Application.Octet);
mm.Attachments.Add(attachment);
}
}
try
{
client.Send(mm);
MessageBox.Show("邮件发送成功", "成功", MessageBoxButtons.OK);
return true;
}
catch (Exception ex)
{
MessageBox.Show("发送失败" + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return false;
}
}
若为修改时,点击添加、修改发件人对话框确定按钮,程序将清空txt然后重新写入。具体代码为
确定按钮代码
1 private void btnOK_Click(object sender, EventArgs e)
2 {
3 NewInforLine = txtSMTP.Text + " " + txtSenderEmail.Text + " " + txtSenderPwd.Text;
4 Regex regex = new Regex(@"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$"); //验证Email格式
5 if (regex.IsMatch(txtSenderEmail.Text.Trim()) && txtSenderPwd.Text.Trim() != "")
6 {
7 if (arg) //修改发件人
8 {
9 string[] content;
10 string[] content1;
11 try
12 {
13 content = File.ReadAllLines(PathBase + inforpath, Encoding.Default);
14 content1 = new string[content.Length];
15 for (int i = 0; i < content.Length; i++)
16 {
17 if (content[i] != OldInforLine)
18 {
19 content1[i] = content[i];
20 continue;
21 }
22 content1[i] = NewInforLine;
23 }
24
25 FileStream stream2 = File.Open(inforpath, FileMode.OpenOrCreate, FileAccess.Write);
26 stream2.Seek(0, SeekOrigin.Begin);
27 stream2.SetLength(0); //清空txt文件
28 stream2.Close();
29 File.WriteAllLines(PathBase + inforpath, content1);
30 }
31 catch (Exception ex)
32 {
33 MessageBox.Show(ex.Message);
34 }
35 }
36 else //添加发件人
37 {
38 using (FileStream stream = new FileStream(inforpath, FileMode.Append, FileAccess.Write))
39 {
40 using (StreamWriter write = new StreamWriter(stream, Encoding.Default))
41 {
42 string stremail = txtSMTP.Text + " " + txtSenderEmail.Text + " " + txtSenderPwd.Text;
43 write.WriteLine(stremail);
44 }
45 }
46 }
47 ClearContent();
48 this.DialogResult = System.Windows.Forms.DialogResult.OK;
49 }
50 else
51 {
52 MessageBox.Show("邮箱格式不正确", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
53 this.DialogResult = System.Windows.Forms.DialogResult.None;
54 }
55 }
程序结构:

添加收件人按钮弹出

适合初学者,程序里面有注解,比较容易懂。
承接之前的博文,征集了之前博友们的意见,本人做了一些修改,添加了一些功能,也完善了一下现有的功能,
1、之前有调节音量,新增了组合键实现上一曲下一曲,空格播放暂停。
2、增加了换肤功能,当然皮肤图片博友们可以自行更改

3、新增的皮肤中控制按钮使用的Metro风格

4、设置FormBorderStyle为none后,相应的标题栏功能没了,修改后增加了点击任务最小化窗体,只需要引用一下类库
[DllImport("user32.dll", EntryPoint = "GetWindowLong", CharSet = CharSet.Auto)] //管理FormBorderStyle:none
public static extern int GetWindowLong(HandleRef hWnd, int nIndex); //点击任务栏最大化最小化程序
[DllImport("user32.dll", EntryPoint = "SetWindowLong", CharSet = CharSet.Auto)]
public static extern IntPtr SetWindowLong(HandleRef hWnd, int nIndex, int dwNewLong);
然后再Load事件中添加如下代码
//点击任务栏图标实现最大化最小化
int WS_SYSMENU = 0x00080000; // 系统菜单
int WS_MINIMIZEBOX = 0x20000; // 最大最小化按钮
int windowLong = (GetWindowLong(new HandleRef(this, this.Handle), -16));
SetWindowLong(new HandleRef(this, this.Handle), -16, windowLong | WS_SYSMENU | WS_MINIMIZEBOX);
5、增加了进程检测,同时只能运行一个程序,避免了多次误点打开多个播放器的问题。
//处理开启多个程序,检测进程,只允许一个应用程序运行
string mName = Process.GetCurrentProcess().MainModule.ModuleName;
string pName = Path.GetFileNameWithoutExtension(mName);
Process[] pro = Process.GetProcessesByName(pName);
if (pro.Length > 1)
{
//MessageBox.Show("程序已经在运行,无需重复运行。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.Dispose();
Application.ExitThread();
}
这段代码同样在Load事件中
别的都是小改,我也记不清了,文章底部附上源码,供博友们学习。
好多博友说用VS2010打开出问题,反正我在我这没问题。下面是我用VS2010转换的,也没问题。如果用Vs2010的,你们就下载这个吧
冒泡排序程序
namespace BubbleSort
{
class BubbleSortTest
{
/// <summary>
/// 使用嵌套循环实现升序排序
/// </summary>
/// <param name="arrary"></param>
public static void Sort(int[] arrary)
{
int temp = 0;
for (int i = 0; i < arrary.Length - 1; i++)
{
for (int j = 0; j < arrary.Length - 1 - i; j++)
{
if (arrary[j] > arrary[j + 1]) //将“>”改成“<”就实现降序排序
{
temp = arrary[j];
arrary[j] = arrary[j+1];
arrary[j + 1] = temp;
}
}
}
}
}
class Program
{
static void Main(string[] args)
{
int[] arrary = { 6, 4, 7, 2, 9, 1, 5, 12, 35, 74, 14, 57, 25, 75, 26, 95, 74, 35, 38, 73 };
BubbleSortTest.Sort(arrary);
foreach (int index in arrary)
{
Console.Write(index + "\t");
}
Console.ReadKey();
}
}
}
队列程序
namespace Queue
{
class QueueTest
{
int[] arrary;
int length = 0;
public QueueTest(int num)
{
arrary = new int[num];
}
public int Length
{
get
{
return this.length;
}
}
public void Push(int num) //数据进队列的方法
{
if (length > 0)
{
for (int i = length - 1; i >= 0; i--) //使用循环将已有的数据后移一个位置,将第一个位置腾出来放置刚进入队列的数据,读数据时从后面读取实现数据的先进先出,后进后出
{
arrary[i + 1] = arrary[i];
}
}
arrary[0] = num;
length++;
}
public int Pop() //数据出队列的方法
{
return arrary[--length]; //取出最后面的一个数
}
}
class Program
{
static void Main(string[] args)
{
QueueTest queuetest = new QueueTest(100);
queuetest.Push(4);
queuetest.Push(8);
queuetest.Push(6);
queuetest.Push(17);
queuetest.Push(2);
queuetest.Push(1);
queuetest.Push(9);
while (queuetest.Length > 0)
{
Console.Write(queuetest.Pop() + "\t");
}
Console.ReadKey();
}
}
}
堆栈程序
namespace Stack
{
class StackTest
{
int[] arrary;
int length = 0;
public StackTest(int num)
{
arrary = new int[num];
}
public int Length
{
get
{
return this.length;
}
}
public void Push(int num) //数据进堆栈的方法,将新数据放入旧数据的后面,Pop()时从后面读取数,实现先进后出,后进先出
{
arrary[length] = num;
length++;
}
public int Pop() //数据出堆栈的方法
{
return arrary[--length]; //取出最后面的一个数
}
}
class Program
{
static void Main(string[] args)
{
StackTest stacktest = new StackTest(100);
stacktest.Push(4);
stacktest.Push(8);
stacktest.Push(6);
stacktest.Push(17);
stacktest.Push(2);
stacktest.Push(1);
stacktest.Push(9);
while (stacktest.Length > 0)
{
Console.Write(stacktest.Pop() + "\t");
}
Console.ReadKey();
}
}
}


