0

我有一个 WinForms 应用程序,用于检查应用程序目录中的 TXT 文件。将只有一行(用户的电子邮件)或没有。代码是这样的:

public static string GetUserEmail()
    {
        string path = Application.StartupPath + "\\mail.txt";
        MessageBox.Show(path);
       string adres = String.Empty;
       if (File.Exists(path))
        {
            using (StreamReader sr = new StreamReader(path))
            {
                adres = sr.ReadLine();
            }
        }
       else
       {
           using (FileStream fs = File.Create(path))
           {
               using (StreamReader sr = new StreamReader(path))
               {
                   adres = sr.ReadLine();
               }
           }

       }
       MessageBox.Show(adres);
        return adres;
    }

除了一种非常奇怪的行为外,这似乎有效。当我卸载程序并重新安装时,它仍然会找到该文件并阅读以前的电子邮件。我检查了 ApplicationDirectory 没有这样的文件,搜索了 Windows,整个 C 驱动器,没有 mail.txt 但它仍然找到并读取了我在第一次安装时输入的邮件地址。提前感谢您的帮助。

4

1 回答 1

0

首先,您正在使用Application.Startup. 这将返回可执行文件所在的路径。因此,不存在将文件查找到其他位置的问题。正如您所说,您搜索了整个C盘,这确实是一个奇怪的问题。我在使用 Windows Vista 和 Win 7 时遇到了类似的问题。在这两个操作系统中,有时文件也会被复制到“SysWow”文件夹中。

于 2009-12-11T12:30:57.157 回答