我有一个 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 但它仍然找到并读取了我在第一次安装时输入的邮件地址。提前感谢您的帮助。