Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#将文件保存在应用程序的位置_C#_Filestream_Streamwriter - Fatal编程技术网

C#将文件保存在应用程序的位置

C#将文件保存在应用程序的位置,c#,filestream,streamwriter,C#,Filestream,Streamwriter,我正在尝试保存到如下文件: string path = AppDomain.CurrentDomain.BaseDirectory; fs = new System.IO.FileStream(path, System.IO.FileMode.Append, System.IO.FileAccess.Write); 但我得到的是DirectoryNotFoundException,尽管目录存在 这里是方法 public static void WriteToFile(strin

我正在尝试保存到如下文件:

    string path = AppDomain.CurrentDomain.BaseDirectory;
    fs = new System.IO.FileStream(path, System.IO.FileMode.Append, System.IO.FileAccess.Write);
但我得到的是DirectoryNotFoundException,尽管目录存在

这里是方法

public static void WriteToFile(string s)
{
    string path = AppDomain.CurrentDomain.BaseDirectory;
    fs = new System.IO.FileStream(path, System.IO.FileMode.Append, System.IO.FileAccess.Write);
    sw = new System.IO.StreamWriter(fs);
    sw.WriteLine(s);
    sw.Flush();
    sw.Close();
    fs.Close();
}

我不知道为什么会这样。谢谢

您需要指定一个文件名,仅指定目录不足以创建文件:

public static void WriteToFile(string s, string fileName)
{
    string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName);
    fs = new System.IO.FileStream(path, System.IO.FileMode.Append, System.IO.FileAccess.Write);
    sw = new System.IO.StreamWriter(fs);
    sw.WriteLine(s);
    sw.Flush();
    sw.Close();
    fs.Close();
}
用法:

WriteToFile("Some Text To Write", "SampleFileName.txt");
以下是以下文件:

public FileStream(string path, FileMode mode, FileAccess access);
参数: 路径:当前FileStream对象将封装的文件的相对或绝对路径


您需要指定文件名,仅指定目录不足以创建文件:

public static void WriteToFile(string s, string fileName)
{
    string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName);
    fs = new System.IO.FileStream(path, System.IO.FileMode.Append, System.IO.FileAccess.Write);
    sw = new System.IO.StreamWriter(fs);
    sw.WriteLine(s);
    sw.Flush();
    sw.Close();
    fs.Close();
}
用法:

WriteToFile("Some Text To Write", "SampleFileName.txt");
以下是以下文件:

public FileStream(string path, FileMode mode, FileAccess access);
参数: 路径:当前FileStream对象将封装的文件的相对或绝对路径


您需要指定文件名,仅指定目录不足以创建文件:

public static void WriteToFile(string s, string fileName)
{
    string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName);
    fs = new System.IO.FileStream(path, System.IO.FileMode.Append, System.IO.FileAccess.Write);
    sw = new System.IO.StreamWriter(fs);
    sw.WriteLine(s);
    sw.Flush();
    sw.Close();
    fs.Close();
}
用法:

WriteToFile("Some Text To Write", "SampleFileName.txt");
以下是以下文件:

public FileStream(string path, FileMode mode, FileAccess access);
参数: 路径:当前FileStream对象将封装的文件的相对或绝对路径


您的路径是一个目录,但System.IO.FileStream.FileStream(字符串路径、FileMode模式、FileAccess访问)需要一个文件路径,因此我修改您的代码如下:

public static void WriteToFile(string s)
    {
        string path = AppDomain.CurrentDomain.BaseDirectory + "a.txt";
        var fs = new System.IO.FileStream(path, System.IO.FileMode.Append, System.IO.FileAccess.Write);
        var sw = new System.IO.StreamWriter(fs);
        sw.WriteLine(s);
        sw.Flush();
        sw.Close();
        fs.Close();
        Console.WriteLine(path);
        Console.ReadKey();
    }
输出: d:\users\zwguo\documents\visual studio 2013\Projects\ConsoleApplication15\Console
eApplication15\bin\Debug\a.txt

您的路径是一个目录,但System.IO.FileStream.FileStream(字符串路径、FileMode模式、FileAccess)需要一个文件路径,因此我修改您的代码如下:

public static void WriteToFile(string s)
    {
        string path = AppDomain.CurrentDomain.BaseDirectory + "a.txt";
        var fs = new System.IO.FileStream(path, System.IO.FileMode.Append, System.IO.FileAccess.Write);
        var sw = new System.IO.StreamWriter(fs);
        sw.WriteLine(s);
        sw.Flush();
        sw.Close();
        fs.Close();
        Console.WriteLine(path);
        Console.ReadKey();
    }
输出: d:\users\zwguo\documents\visual studio 2013\Projects\ConsoleApplication15\Console
eApplication15\bin\Debug\a.txt

您的路径是一个目录,但System.IO.FileStream.FileStream(字符串路径、FileMode模式、FileAccess)需要一个文件路径,因此我修改您的代码如下:

public static void WriteToFile(string s)
    {
        string path = AppDomain.CurrentDomain.BaseDirectory + "a.txt";
        var fs = new System.IO.FileStream(path, System.IO.FileMode.Append, System.IO.FileAccess.Write);
        var sw = new System.IO.StreamWriter(fs);
        sw.WriteLine(s);
        sw.Flush();
        sw.Close();
        fs.Close();
        Console.WriteLine(path);
        Console.ReadKey();
    }
输出: d:\users\zwguo\documents\visual studio 2013\Projects\ConsoleApplication15\Console
eApplication15\bin\Debug\a.txt

很有可能
path
指向目录(大约100%)。您能否澄清一下,为什么希望目录路径的文件创建成功?很有可能
path
指向目录(大约100%)。您能否澄清一下,为什么希望目录路径的文件创建成功?很有可能
path
指向目录(大约100%)。请您澄清为什么希望目录路径的文件创建成功?您应该始终将路径部分与
路径合并。合并
您应该始终将路径部分与
路径合并。合并
您应该始终将路径部分与
路径合并。合并