【发布时间】:2016-10-13 10:49:43
【问题描述】:
如果我使用它正在工作的完整路径,我在 .txt 文件上有一个 DirectoryNotFoundException 但我不想使用完整路径,因为我希望程序无论放在哪里都可以工作(与最大计算机兼容)
这是我的代码
private void SaveClose_Click(object sender, RoutedEventArgs e)
{
if (Windowed.IsChecked == true)
windowed = true;
else
windowed = false;
string textWriteWindowed;
if (windowed == true)
{
textWriteWindowed = "-screen-fullscreen 0" + Environment.NewLine;
}
else
{
textWriteWindowed = "-screen-fullscreen 1" + Environment.NewLine;
}
var selectedResolution = ResolutionBox.SelectedItem.ToString();
var split = selectedResolution.Split('x');
widthChoose = Int32.Parse(split[0]);
heightChoose = Int32.Parse(split[1]);
string textWriteWidth;
textWriteWidth = "-screen-width " + widthChoose + Environment.NewLine;
string textWriteHeight;
textWriteHeight = "-screen-height " + heightChoose + Environment.NewLine;
File.WriteAllText(@"\Resources\arguments.txt", textWriteWindowed);
File.AppendAllText(@"\Resources\arguments.txt", textWriteWidth);
File.AppendAllText(@"\Resources\arguments.txt", textWriteHeight);
this.Close();
}
【问题讨论】:
-
当您立即投反对票时 ;-;
-
先试试看目录是否存在Directory.Exists method
-
“arguments.txt”在我的程序旁边一个名为“Ressources”的文件夹中我不明白为什么它告诉我,我应该怎么写?
-
试试 File.WriteAllText("../../Resources/arguments.txt", textWriteWindowed);
-
不要使用“../../”作为相对路径的开始,因为这只在 Visual Studio 项目的默认输出目录结构中才有意义,输出生成到例如“bin/Debug/”(甚至可以更改)。为了让它在任何地方都能工作,如果应用程序的当前工作目录不存在,您应该在应用程序的当前工作目录中创建 Resources 目录。