简体   繁体   English

我如何检查文件是否存在并且不为空然后从文件中读取所有行?

[英]How can i check if file exist and not empty then to read all lines from the file?

In a new form top i did: 在新表格的顶部,我做了:

public static string AuthenticationApplicationDirectory;
public static string AuthenticationFileName = "Authentication.txt";

Then in the new form constructor i did: 然后在新的表单构造函数中,我做了:

AuthenticationApplicationDirectory = Path.GetDirectoryName(Application.LocalUserAppDataPath) + "Authentication";
            if (!Directory.Exists(AuthenticationApplicationDirectory))
            {
                Directory.CreateDirectory(AuthenticationApplicationDirectory);
            }
            AuthenticationFileName = Path.Combine(AuthenticationApplicationDirectory,AuthenticationFileName);

Then in form1 Load event: 然后在form1中加载事件:

private void Form1_Load(object sender, EventArgs e)
        {
            Authentication.AuthenticationFileName = Path.Combine(Authentication.
                AuthenticationApplicationDirectory, Authentication.AuthenticationFileName);
            if (File.Exists(Authentication.AuthenticationFileName) &&
                new FileInfo(Authentication.AuthenticationFileName).Length != 0)
            {
                string[] lines = File.ReadAllLines(Authentication.AuthenticationFileName);
            }
            else
            {
                Authentication auth = new Authentication();
                auth.Show(this);
            }
        }

But getting exception in form1 load event that AuthenticationApplicationDirectory is null. 但是在form1加载事件中出现异常,即AuthenticationApplicationDirectory为null。

What i want to do is once if the file not exist or empty make instance and show the new form. 我想做的是一次,如果文件不存在或为空的make实例并显示新表单。

If the file exist and not empty then read the lines from it into string[] lines. 如果该文件存在并且不为空,则将其中的各行读为string []行。

The problem is not How can I check if file exist and not empty then to read all lines from the file? 问题不在于如何检查文件是否存在并且不为空然后从文件中读取所有行? in fact it is Why my static member is null while I have initialized it? 实际上,这就是为什么初始化后我的静态成员为null?

It seems you have put the code that initializes your static members in the Authentication class constructor, so before you initialize an instance of Authentication form, that code will not run and AuthenticationApplicationDirectory is null. 似乎您已经将用于初始化静态成员的代码放入Authentication类构造函数中,因此在初始化Authentication表单实例之前,该代码将不会运行,并且AuthenticationApplicationDirectory为null。

You should put your codes in static constructor of that class: 您应该将代码放在该类的静态构造函数中:

public class Authentication : Form
{
    public static string AuthenticationApplicationDirectory;
    public static string AuthenticationFileName = "Authentication.txt";

    static Authentication()
    {
        AuthenticationApplicationDirectory = Path.GetDirectoryName(Application.LocalUserAppDataPath) + "Authentication";
        if (!Directory.Exists(AuthenticationApplicationDirectory))
        {
            Directory.CreateDirectory(AuthenticationApplicationDirectory);
        }
        AuthenticationFileName = Path.Combine(AuthenticationApplicationDirectory, AuthenticationFileName);
    }

    public Authentication()
    {
         InitializeComponent();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM