英文:
C# - How to create a text file and add a line of text to it?
问题
以下是您要翻译的内容:
我有一个创建文件的C#脚本,我想更新它以向文件添加一些文本。
这是完美创建文件的代码:
String filepath = "";
filepath = "This/is/a/dynamic/file/path.txt";
System.IO.File.Create(filepath);
我想添加一些代码来向文件添加一行文本。以下是我尝试过的,但当我运行它时,我收到以下错误:
错误:进程无法访问文件 'This/is/a/dynamic/file/path.txt',因为它正在被另一个进程使用。
String filepath = "";
filepath = "This/is/a/dynamic/file/path.txt";
System.IO.File.Create(filepath);
System.IO.File.AppendAllText(filepath, "Text to add to file" + Environment.NewLine);
任何帮助将不胜感激,如果这是一个愚蠢的问题,请原谅,因为我对C#非常陌生。
英文:
I have a C# script that creates a file and I would like to update it to add some text to the file.
This is the code that works perfectly to create the file:
String filepath = "";
filepath = "This/is/a/dynamic/file/path.txt";
System.IO.File.Create(filepath);
I would like to add some code to add a line of text to the file. Below is what I have tried, but when I run it I get this error:
error: the process cannot access the file 'This/is/a/dynamic/file/path.txt' because it is being used by another process.
String filepath = "";
filepath = "This/is/a/dynamic/file/path.txt";
System.IO.File.Create(filepath);
System.IO.File.AppendAllText(filepath, "Text to add to file" + Environment.NewLine);
Any help would be much appreciated, and forgive me if this is a dumb question as I'm very new to C#.
答案1
得分: 0
当您使用File.Create
时,它会返回一个FileStream
,它是IDisposable
,您应该始终使用using语句确保它被释放。这将确保它释放文件。
using FileStream file = File.Create(filepath);
然而,File.AppendAllText
如果文件不存在将创建文件,它不会返回一个流,并在完成后关闭文件,因此无需担心,只需完全删除创建文件的行即可。
https://learn.microsoft.com/en-us/dotnet/api/system.io.file.appendalltext?view=net-7.0
英文:
When you use File.Create
it returns a FileStream
which is IDisposable
, you should always use a using statement to ensure it is disposed. This will ensure it releases the file.
using FileStream file = File.Create(filepath);
However, File.AppendAllText
will create the file if it doesn't exist and does not return a Stream and it closes the file when done, so no need to worry, just remove the create line all together.
https://learn.microsoft.com/en-us/dotnet/api/system.io.file.appendalltext?view=net-7.0
答案2
得分: 0
你之前提到,文件创建后立即未使用.Close()
。
我建议改用StreamWriter
,它会在文件不存在时创建,或者写入已存在的文件。它还支持追加或覆盖模式。
using (StreamWriter outputFile = new StreamWriter("This/is/a/dynamic/file/path.txt", true))
{
outputFile.WriteLine("Text to add to file");
}
英文:
As mentioned yearlier you missed .Close()
immediately after file creation.
Instead I suggest using StreamWriter
that will create if doesn't exists, or write into existing file. It also support append or override modes
using (StreamWriter outputFile = new StreamWriter("This/is/a/dynamic/file/path.txt", true)))
{
outputFile.WriteLine("Text to add to file");
}
答案3
得分: 0
你的文件仍然被File.Create()
方法使用(或保持打开状态)。因此,在创建文件后,您需要关闭它。
File.Create(filepath).Close();
File.AppendAllText(filepath, "Your text\n");
英文:
Your file is still being used (or kept open) by the File.Create()
method. So you need to close it after creating the file.
File.Create(filepath).Close();
File.AppendAllText(filepath, "Your text\n");
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论