马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
通讯汗青按钮- private void uiButton1_Click(object sender, EventArgs e)
- {
- try
- {
- logf = new logF();
- logf.Show();
- }
- catch (Exception)
- {
- throw;
- }
- }
复制代码 主页面关闭函数(点击生存就为true true就不删除)- private void page1_FormClosed(object sender, FormClosedEventArgs e)
- {
- // 关闭应用时删除txt文件 为true就不删除
- if (logF.savelog== false)
- {
- if (File.Exists(logF.filePath))
- {
- File.Delete(logF.filePath);
- }
- }
- Application.Exit();
- }
复制代码 log.cs
 - using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace WindowsFormsApp1
- {
- internal class log
- {
- public static void SaveLog(string log, string filename = @"C:\log\run.txt")
- {
- string pathA = filename.Substring(0, filename.LastIndexOf("\"));
- if (!Directory.Exists(pathA))
- {
- DirectoryInfo directoryInfo = new DirectoryInfo(pathA);
- directoryInfo.Create();
- }
- string filepathA = filename;
- try
- {
- String time = DateTime.Now.ToString("[yyyy-MM-dd hh:mm:ss]");
- time += log;
- if (!File.Exists(filepathA))
- {
- using (StreamWriter sw = File.CreateText(filepathA))
- {
- sw.WriteLine(time);
- sw.Close();
- }
- }
- else
- {
- {
- using (StreamWriter sw = File.AppendText(filepathA))
- {
- sw.WriteLine(time);
- sw.Close();
- }
- }
- }
- }
- catch
- {
- // MessageBox.Show("log异常" + ee.ToString());
- }
- }
- }
- }
复制代码 |