public static void myFileWrite(string msg, string path)
{
byte[] myByte = System.Text.Encoding.UTF8.GetBytes(msg);
using (FileStream fsWrite = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.ReadWrite))
{
fsWrite.Write(myByte, 0, myByte.Length);
};
}
public static string myFileRead(string path)
{
string contents = "xxx";
// FileStream fileRead = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read);
using (FileStream fileRead = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read, FileShare.ReadWrite))
{
byte[] b = new byte[(int) fileRead.Length];
int r = fileRead.Read(b, 0, b.Length);
contents = Encoding.UTF8.GetString(b);
};
return contents;
}
//查看文件是否存在
public static bool MyFileExists(string path)
{
path = AppDomain.CurrentDomain.BaseDirectory + “data.txt”;
//文件路径,AppDomain.CurrentDomain.BaseDirectory为程序所在位置,data.txt为查找的目标文件
if (System.IO.File.Exists(path))//查看文件是否存在
{
MessageBox.Show(“文件存在”);
return true;
}
else
{
MessageBox.Show(“文件不存在”);
return false;
}
}