主要就是用DateTime.Now.ToString("yyyyMMddHHmmss")和DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")去格式化时间,用StreamWriter去创建文件,用File.Copy复制文件,用File.Delete删除文件,代码见附件中。
//主代码
private void btnSaveFile_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(txtDateTimeName.Text))
{
MessageBox.Show("请先输入时间格式的名称:格式yyyyMMddHHmmss,如:20130145121501");
return;
}
try
{
DateTime dt = DateTime.ParseExact(txtDateTimeName.Text, "yyyyMMddHHmmss", null, DateTimeStyles.None);
SaveFileDialog dialog = new SaveFileDialog();
dialog.Filter = "文本文件|*.txt";
dialog.FilterIndex = 0;
dialog.FileName = txtDateTimeName.Text;
if (dialog.ShowDialog() == DialogResult.OK)
{
StringBuilder strmessage = new StringBuilder();
strmessage.AppendLine(new FileInfo(dialog.FileName).Name);
strmessage.AppendLine(dt.ToString("yyyy-MM-dd HH:mm:ss"));
strmessage.AppendLine("----------------------------------");
strmessage.AppendLine(txtBody.Text);
if (!dialog.CheckPathExists)
Directory.CreateDirectory(dialog.FileName);
File.AppendAllText(dialog.FileName, strmessage.ToString());
labFileAddress.Text = dialog.FileName;
MessageBox.Show("保存成功");
}
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
return;
}
}
private void btnDelFile_Click(object sender, EventArgs e)
{
//此处原代码有问题,需要添加!
if (!string.IsNullOrEmpty(labFileAddress.Text))
{
File.Delete(labFileAddress.Text);
labFileAddress.Text = string.Empty;
MessageBox.Show("删除成功");
}
}
帮你写了个类,调用就行
using System;
using System.IO;
namespace MateralTools.File
{
public class FileOperator
{
///
/// 保存Text(我非常不理解为何要创建了在复制,你直接创建进去不是更好吗?)
///
/// 文件夹路径
/// 时间字符串
/// 数字参数列表
public void SaveText(string filePath, string dateTime, params int[] para)
{
if (dateTime.Length == 14)
{
int Y = Convert.ToInt32(dateTime.Substring(0, 4));
int M = Convert.ToInt32(dateTime.Substring(4, 2));
int D = Convert.ToInt32(dateTime.Substring(6, 2));
int H = Convert.ToInt32(dateTime.Substring(8, 2));
int m = Convert.ToInt32(dateTime.Substring(10, 2));
int s = Convert.ToInt32(dateTime.Substring(12, 2));
DateTime dt = new DateTime(Y, M, D, H, m, s);
DirectoryInfo di = new DirectoryInfo(filePath);
if (!di.Exists)
{
di.Create();
}
filePath += "/" + dateTime + ".TXT";
FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate);//这样是追加如果不想要追加就把FileModel换成CreateNew
String str = String.Format("{0}.TXT\r\n{1} {2}\r\n--- \r\n", dateTime, dt.ToShortDateString(), dt.ToShortTimeString());
foreach (int temp in para)
{
str += temp.ToString() + "\r\n";
}
StreamWriter sw = new StreamWriter(fs);
sw.BaseStream.Seek(0, SeekOrigin.End);
sw.WriteLine(str);
sw.Flush();
sw.Close();
if (fs != null)
{
fs.Close();
}
}
}
///
/// 删除Text
///
/// 文件路径
public void DeleteText(string filePath)
{
if (System.IO.File.Exists(filePath))
{
System.IO.File.Delete(filePath);
}
}
}
}
时间那个可以用DateTime类。
DateTime dt = DateTime.Now;
第一行:
string s = dt.Year + dt.Month.ToString().PadLeft(2, '0') + dt.Day.ToString().PadLeft(2, '0') + dt.Hour.ToString().PadLeft(2, '0') + dt.Minute.ToString().PadLeft(2, '0') + dt.Second.ToString().PadLeft(2, '0');
第二行:
string s1 = dt.Year + "-" + dt.Month.ToString().PadLeft(2, '0') + "-" + dt.Day.ToString().PadLeft(2, '0') + " " + dt.Hour.ToString().PadLeft(2, '0') + ":" + dt.Minute.ToString().PadLeft(2, '0') + ":" + dt.Second.ToString().PadLeft(2, '0');
然后可以用File.WriteAllText()方法写入文件。
复制到文件夹可以用File.Copy()方法
忘了说,用户输入若干行,写入的时候一定把 \n 替换成 \r\n,要不然在文本里面会出现黑色的小框框。