private void button6_Click(object sender, EventArgs e)
{
// 把指定目录 AppLog.path 下的日志文件存放到用户选择的目录下
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "日志|*.log";
// 获取要保存的文件文件名
saveFileDialog.FileName = Path.GetFileName(AppLog.path);
// 设置默认的保存路径为桌面
// saveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
// 记录上次打开位置
saveFileDialog.RestoreDirectory = true;
// 直接打开日志文件
// System.Diagnostics.Process.Start(AppLog.path);
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
string localPath = saveFileDialog.FileName.ToString();
// string filename = localPath.Substring(localPath.LastIndexOf("\\") + 1);
string savePath = localPath.Substring(0, localPath.LastIndexOf("\\"));
saveFileDialog.InitialDirectory = savePath;
string filename = Path.GetFileName(AppLog.path);
FileStream fileread = new FileStream(AppLog.path, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fileread, System.Text.Encoding.Default);
StringBuilder sb = new StringBuilder();
while (!sr.EndOfStream)
{
sb.AppendLine(sr.ReadLine());
}
FileStream fs = File.Create(saveFileDialog.InitialDirectory + "\\" + filename);
byte[] logByte = System.Text.Encoding.Default.GetBytes(sb.ToString());
fs.Write(logByte, 0, logByte.Length);
fs.Close();
MessageBox.Show("日志保存成功");
}
}
c#保存日志文件
最新推荐文章于 2024-06-21 18:55:31 发布