【发布时间】:2022-12-18 09:36:55
【问题描述】:
它仅在我使用绝对路径时有效,但我希望它是相对的。我试过 AppDomain.CurrentDomain.BaseDirectory 但它转到了bin文件夹文件不在的地方,它们在项目文件夹.
我应该怎么办?
try
{
using StreamReader reader = new("input.txt");
using StreamWriter writer = new("resources\\output.txt");
string line;
while ((line = reader.ReadLine()) != null)
{
writer.WriteLine(line);
}
reader.Close();
writer.Close();
}
catch (FileNotFoundException)
{
MessageBox.Show("File Not Found");
}
【问题讨论】:
-
将文件添加到您的解决方案中,并针对它们的每个属性要求将它们复制到输出目录(总是或更新)对您有用吗?因为通常你只部署 bin/Debug 文件夹中的内容,而不是上面的内容。
-
我建议尝试使用
Path.GetFullPath(...),这样您就可以看到您的相对路径实际解析为什么。 -
我要读取或写入的所有文件都必须在bin文件夹?
-
是的,正在运行的应用程序不知道任何项目文件夹。看我的回答。