【问题标题】:How can I fix this DirectoryNotFoundException?如何修复此 DirectoryNotFoundException?
【发布时间】: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 目录。

标签: c# wpf path


【解决方案1】:

File.WriteAllText 的第一个参数将路径作为输入。您提到的任何内容都不是绝对路径,而只是文件的相对路径。 WriteAllText 创建文件但不自己创建目录。所以像:

File.WriteAllText(@"\arguments.txt", textWriteWindowed);

应该可以工作(并在相应的驱动器中创建文件),但是

File.WriteAllText(@"\Resources\arguments.txt", textWriteWindowed);

将无法正常工作。因此,如果您想在应用程序所在的路径中创建一个文件,您可以执行以下操作:

string folder=Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
File.WriteAllText(@"\arguments2.txt", "ABC");

如果你想创建一个目录,那么你可以这样做:

System.IO.FileInfo file = new System.IO.FileInfo(filePath);
file.Directory.Create();// If the directory already exists, this method does nothing.

System.IO.File.WriteAllText(file.FullName, textWriteWindowed);

希望这能回答您的问题。

【讨论】:

  • 您好,谢谢您的回复,ABC 是干什么用的?以及为什么“arguments2.txt”
【解决方案2】:

你必须在保存文件之前检查文件夹是否存在,

如果文件夹不存在,使用创建它

Directory.CreateDirectory(...)

Directory.Exists(..) 

你可以用来检查文件夹是否存在

【讨论】:

  • 我不知道去哪里 ^^'
【解决方案3】:

如果您想获取正在执行的文件的本地路径,请使用:

 var fInfo = new FileInfo(System.Reflection.Assembly.GetCallingAssembly().Location);

从那里,您将执行以下操作:

var parentDir = new DirectoryInfo(fInfo.DirectoryName);
var subDir = new DirectoryInfo(parentDir.FullName + "Resource");
if(!subDir.Exists)
   subDir.Create();

这将确保您的可执行文件目录中始终有一个文件夹。但是你知道,这绝对是可怕的代码,永远不应该在类似生产环境中实现。如果某些笨拙的系统管理员决定将您的程序/文件夹放在当前用户也无权访问/写入的区域怎么办?写入的最佳位置是 %APPDATA%,这将确保用户始终对您要完成的任务具有读/写权限。

【讨论】:

    【解决方案4】:

    我不知道怎么做,但这样做对我有用:

     File.WriteAllText(@"./arguments.txt", textWriteWindowed);
                File.AppendAllText(@"./arguments.txt", textWriteWidth);
                File.AppendAllText(@"./arguments.txt", textWriteHeight);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-11
      • 2010-12-22
      • 2011-12-30
      • 2021-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多