Skip to main content
 首页 » 编程设计

c#之首次运行时跳过写入文件

2024年02月23日14傻小

我是 C# 的新手,我有一些代码可以从 LogIn 获取用户名,如果这是用户第一次登录系统,则可以在后台启动一个程序。但是,今天我在运行该程序并检查我的日志文件时注意到,该程序跳过了向同样在初始运行时创建的文件添加数据的操作。初始运行后的任何运行,用户名都包含在 .log 文件中,如果用户名与 .log 文件中的内容不匹配,则该文件将被覆盖以包含新的用户名。有人可以帮我弄清楚发生了什么,或者我是否遗漏了什么?

提前谢谢你。

 using System; 
    using System.Collections.Generic; 
    using System.ComponentModel; 
    using System.Data; 
    using System.Drawing; 
    using System.Linq; 
    using System.Text; 
    using System.Windows.Forms; 
    using System.IO; 
    using System.Diagnostics; 
 
    namespace User 
    { 
        public partial class Form1 : Form 
        { 
            public const string dir = @"C:\Numara"; 
            public const string path = dir + @"\Audit.log"; 
            public const string TrackIT = @"C:\Program Files\Numara Software\Numara Track-It!\Track-It! Server\Audit32.exe"; 
 
            public Form1() 
            { 
                InitializeComponent(); 
            } 
 
            private void Form1_Load(object sender, EventArgs e) 
            { 
                //returns user name 
                //label1.Text = System.Environment.UserName.ToString(); 
                string userName = System.Environment.UserName;  //user name 
 
                if (!Directory.Exists(dir)) 
                    //directory does not exist 
                    //create it 
                    Directory.CreateDirectory(dir);  //creates directory 
 
 
                //by this point directory is created 
 
                //now check file 
                if (!File.Exists(path)) 
                    //file does not exist, so create it 
                    File.Create(path); 
 
 
                //Read data from the .log file 
                string line = System.IO.File.ReadAllText(path); 
 
                //if the name of the logged in user 
                //is the same as the user name of the text file 
                //then exit program 
 
--------- the debugging stops here and skips to end the program on the first run--------- 
 
                if (line == userName)  
                    Application.Exit(); 
 
                else 
                //clear fields and write new name to file and begin audit 
                { 
                    //clears fields 
                    using (FileStream stream = new FileStream(@"C:\Numara\Audit.log", FileMode.Create)) 
                    { 
                        using (TextWriter writer = new StreamWriter(stream)) 
                        { 
                            //writer.Write(""); 
                            writer.Write(userName); 
                        } 
                        // writes new name to file 
                    } 
 
                      StreamReader textIn = 
                      new StreamReader( 
                      new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read)); 
 
 
                    //begins audit 
 
                    Process.Start(TrackIT, "/Q"); 
                    Application.Exit(); 
                    }         
                } 
            } 
        } 

请您参考如下方法:

File.Create 创建一个文件并返回一个 FileStream,因此它不会关闭它。接下来您想读取它,但它被 File.Create 锁定。因此出现未处理的异常并且您的应用程序退出。第二次该文件已创建,因此它会跳过创建和锁定它。

因此您需要将文件放入 using block 来关闭该文件: 使用 (File.Create(path)) { }

此外,您应该将所有内容放入 try catch block 中以处理 IO 异常 - 您永远无法知道是否发生了异常。