gpt4 book ai didi

c# - 将文件写入任何计算机上的项目文件夹

转载 作者:可可西里 更新时间:2023-11-01 08:54:03 25 4
gpt4 key购买 nike

我正在为一个类(class)做一个项目。我要做的是将解析后的指令导出到一个文件中。微软有这个例子解释了如何写入文件:

// Compose a string that consists of three lines.
string lines = "First line.\r\nSecond line.\r\nThird line.";

// Write the string to a file.
System.IO.StreamWriter file = new System.IO.StreamWriter("c:\\test.txt");
file.WriteLine(lines);

file.Close();

我没问题,但是有没有办法将文件写入当前项目的环境/位置?我想这样做而不是硬编码特定路径(即 "C:\\test.txt")。

最佳答案

是的,只是使用相对路径。如果你使用 @".\test.txt"(顺便说一下,@ 只是说我正在做一个字符串文字,它消除了对转义字符的需要,所以你也可以做 "。\\test.txt" 并且它会写入同一个地方)它会将文件写入当前工作目录,在大多数情况下是包含您的程序的文件夹。

关于c# - 将文件写入任何计算机上的项目文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19329672/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com