如何把内容写入文本呢?新手求救
我刚学winform不久,现在有个难题了,就是要把某人操作的这个软件的操作记录写到文本里面去,格式是" 用户 时间 做了什么操作 操作的数据是什么",总的来说就是把“增加”“删除”“改”(不用记录“查询”的操作),我看了一下那些什么读写流,搞的我头晕,我没多少时间研究了,有没现成的代码呀,应该没多少代码的吧,
给点指导呗。。谢谢!!!最好不要覆盖吧,每次操作追加就可以了
--------------------编程问答-------------------- 如果是很简单的文件读写操作:
--------------------编程问答--------------------
string filePath="E:\1.log";
string content="2010-3-30 16:50:50 增加了一条数据";
System.IO.File.AppendAllText(filepath,content);
如何获取系统的路径啊,因为用户把软件安装在哪里,我们根本无法预测呀 --------------------编程问答--------------------
//将文件指定的文件转换成二进制数组
/// <summary>--------------------编程问答-------------------- string exePath = System.AppDomain.CurrentDomain.BaseDirectory;
/// Files the content.
/// </summary>
/// <param name="fileName">Name of the file.</param>
/// <returns></returns>
private byte[] FileContent(string fileName)
{
FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
try
{
byte[] buffur = new byte[fs.Length];
fs.Read(buffur, 0, (int)fs.Length);
return buffur;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return null;
}
finally
{
if (fs != null)
{
fs.Close();
}
}
}
当前程序运行的目录 --------------------编程问答-------------------- 对于文件流的操作,首先你得引用命名空间:using System.IO;
如果你是建一个空文件的话,可以使用:File.Create(@"C:\test.txt");
如果你需要往文件里面写入信息的话,可以使用FileStream和StreamWriter:
//初始化文件流,指定文件路径,文件被打开的方式,文件访问模式--------------------编程问答-------------------- C#追加文件
//FileMode和FileAccess是枚举类型
FileStream fs = new FileStream("C:\\test.txt", FileMode.Create, FileAccess.Write);
//初始化写入流对象,指定要写入的流,编码方式
StreamWriter sw = new StreamWriter(fs, Encoding.Default);
//向文件写入内容
sw.WriteLine("this is a test information");
//关闭流,这个一定要关闭
fs.Close();
sw.Close();
StreamWriter sw = File.AppendText(Server.MapPath(".")+"\\myText.txt");
sw.WriteLine("追逐理想");
sw.WriteLine("kzlll");
sw.WriteLine(".NET笔记");
sw.Flush();
sw.Close();
C#拷贝文件
string orignFile,NewFile;
orignFile = Server.MapPath(".")+"\\myText.txt";
NewFile = Server.MapPath(".")+"\\myTextCopy.txt";
File.Copy(OrignFile,NewFile,true);
C#删除文件
string delFile = Server.MapPath(".")+"\\myTextCopy.txt";
File.Delete(delFile);
C#移动文件
string orignFile,NewFile;
orignFile = Server.MapPath(".")+"\\myText.txt";
NewFile = Server.MapPath(".")+"\\myTextCopy.txt";
File.Move(OrignFile,NewFile);
C#创建目录
// 创建目录c:\sixAge
DirectoryInfo d=Directory.CreateDirectory("c:\\sixAge");
// d1指向c:\sixAge\sixAge1
DirectoryInfo d1=d.CreateSubdirectory("sixAge1");
// d2指向c:\sixAge\sixAge1\sixAge1_1
DirectoryInfo d2=d1.CreateSubdirectory("sixAge1_1");
// 将当前目录设为c:\sixAge
Directory.SetCurrentDirectory("c:\\sixAge");
// 创建目录c:\sixAge\sixAge2
Directory.CreateDirectory("sixAge2");
// 创建目录c:\sixAge\sixAge2\sixAge2_1
Directory.CreateDirectory("sixAge2\\sixAge2_1");
本篇文章来源于:开发学院 http://edu.codepub.com 原文链接:http://edu.codepub.com/2009/0621/6859.php --------------------编程问答-------------------- Application.StartupPath得到程序exe运行所在的路径 --------------------编程问答-------------------- 直接放置在软件目录下就可以了.
StreamWriter sw = File.AppendText(System.Environment.CurrentDirectory + @"\Log.Txt");
sw.WriteLine("一个新增动作,时间:{0}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff"));
sw.WriteLine("一个删除动作,时间:{0}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff"));
sw.WriteLine("一个修改动作,时间:{0}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff"));
sw.Flush();
sw.Close();
补充:.NET技术 , C#