C# 在C中找不到DirectoryNotException#

C# 在C中找不到DirectoryNotException#,c#,.net,path,C#,.net,Path,我试图在路径WindowsFormsApplication1\WindowsFormsApplication1\SaveFile处保存文件,但以下代码返回“DirectoryNotFound”异常,并显示以下消息: 找不到路径的一部分 'D:\WindowsFormsApplication1\WindowsFormsApplication1\WindowsFormsApplication1\bin\Debug\SaveFile\Hello.tx 有人能告诉我如何在指定完整路径的情况下将文件保存在

我试图在路径
WindowsFormsApplication1\WindowsFormsApplication1\SaveFile
处保存文件,但以下代码返回“DirectoryNotFound”异常,并显示以下消息:

找不到路径的一部分 'D:\WindowsFormsApplication1\WindowsFormsApplication1\WindowsFormsApplication1\bin\Debug\SaveFile\Hello.tx


有人能告诉我如何在指定完整路径的情况下将文件保存在所需文件夹中吗?

当您在调试器中运行时,默认路径位于
bin\Debug
下。这就是“在你的道路上”的意思


要保存到哪个文件夹?您需要指定完整路径。也许您需要从配置文件中提取路径。这样,路径将能够根据应用程序的部署位置进行更改。

因为错误消息告诉您该文件将保存在bin/debug下的子目录SaveFile中。在保存文件之前,必须使用directory.CreateDirectory(“保存文件”)创建一个目录。它不会自动创建。

在创建文本文件之前,您需要确保目录存在

String Path = @".\SaveFile\Hello.txt";
FileInfo info = new FileInfo(Path);
if (!info.Exists)
{
    if (!info.Directory.Exists)
        info.Directory.Create();

    using (StreamWriter writer = info.CreateText())
    {
        writer.WriteLine("HELLO");
    }
}

这么多层次的错误。如果不想指定要保存文件的文件夹,如何将文件保存到文件夹中?我想将文件保存在“WindowsFormsApplication1\WindowsFormsApplication1\SaveFile”文件夹中,并指定完全限定的URL。@Stardust:然后您需要在当前目录设置为的情况下运行应用程序“D:\WindowsFormsApplication1\WindowsFormsApplication1\WindowsFormsApplication1”。在写入目录之前,您甚至不尝试验证该目录是否存在。
String Path = @".\SaveFile\Hello.txt";
FileInfo info = new FileInfo(Path);
if (!info.Exists)
{
    if (!info.Directory.Exists)
        info.Directory.Create();

    using (StreamWriter writer = info.CreateText())
    {
        writer.WriteLine("HELLO");
    }
}