Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/313.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#如何在Linux中使用绝对路径写入文件_C#_Linux - Fatal编程技术网

C#如何在Linux中使用绝对路径写入文件

C#如何在Linux中使用绝对路径写入文件,c#,linux,C#,Linux,我正在编写一个C代码,在Linux中使用绝对路径写入文件。我在StreamWriter中使用的路径是正确的,但会抛出DirectoryNotFoundException using (StreamWriter streamWriter = new StreamWriter("/home/myname/results/" + settings.runID + "/CoachAgentDecisions.csv", true)) { s

我正在编写一个C代码,在Linux中使用绝对路径写入文件。我在
StreamWriter
中使用的路径是正确的,但会抛出
DirectoryNotFoundException

using (StreamWriter streamWriter = new StreamWriter("/home/myname/results/" + settings.runID + "/CoachAgentDecisions.csv", true))
    {
        streamWriter.WriteLine(totalEpisode);
    }
当我尝试只写相对路径时,它会工作,但我需要写绝对路径


这条路是对的,但我写不出来。我在Linux或C中使用绝对路径的方式是错误的吗?

您不能将文件写入不存在的目录,您可以使用
目录。创建(…)
来创建目录和所需的任何子目录


创建后,您可以向其中写入文件。

您不能将文件写入不存在的目录,您可以使用
目录。创建(…)
来创建目录和所需的任何子目录


创建后,您可以向其中写入文件。

当您使用绝对路径写入文件时,它的工作方式与使用相对路径类似

确保您使用了正确的文件夹路径,没有任何不需要的空间或符号,并在写入之前确认文件夹是否如预期的那样存在

string filePath = $@"/home/myname/results/{settings.runID}";
if(Directory.Exists(filePath)){
    using (StreamWriter streamWriter = new StreamWriter($"{filePath}/output.txt", true))
        streamWriter.WriteLine("Hello World!");
}
else Console.WriteLine("Folder doesn't exist");

当您使用绝对路径写入文件时,它的工作方式将与使用相对路径类似

确保您使用了正确的文件夹路径,没有任何不需要的空间或符号,并在写入之前确认文件夹是否如预期的那样存在

string filePath = $@"/home/myname/results/{settings.runID}";
if(Directory.Exists(filePath)){
    using (StreamWriter streamWriter = new StreamWriter($"{filePath}/output.txt", true))
        streamWriter.WriteLine("Hello World!");
}
else Console.WriteLine("Folder doesn't exist");
但我不能写这样的话:如果你描述一下当你尝试时会发生什么,那将是非常有帮助的。你有例外吗?哪一个?但我不能写:如果你描述一下你尝试时会发生什么,那将非常有帮助。你有例外吗?哪个?