简体   繁体   English

我无法写入添加到项目中的 txt 文件

[英]I can't write to txt file which is added into project

31/12/2014 SOLUTION: change filename = @"../../text.txt" I did it successfully ^^ Thank you so much 31/12/2014 解决方案:更改文件名 = @"../../text.txt" 我成功了^^ 非常感谢


If my text file is determined by a path, for example filename = "C:\\text.txt", I can write data into that file normally.如果我的文本文件是由路径决定的,例如 filename = "C:\\text.txt",我可以正常将数据写入该文件。

But if I add text.txt (determined by filename = "text.txt") into my C# project, my program still run without error but nothing is written into text.txt file但是如果我将 text.txt(由 filename = "text.txt" 确定)添加到我的 C# 项目中,我的程序仍然可以正常运行,但没有任何内容写入 text.txt 文件

Besides I can read txt file (with filename = "text.txt") normally.此外我可以正常读取txt文件(文件名=“text.txt”)。

Did I miss something?我错过了什么? Please help me!请帮我!

FileStream stream = new FileStream(filename, FileMode.Append);
StreamWriter writeRecord = new StreamWriter(stream);

writeRecord.Write("Random string");
writeRecord.WriteLine();            

writeRecord.Close(); 
stream.Close();

Edit: Sorry Sorry.编辑:对不起对不起。 I also mean that When i added text.txt into C# project.我的意思是,当我将 text.txt 添加到 C# 项目中时。 I open file with FileMode.Append and write but data in text.txt file, which i added into project, didn't change我用 FileMode.Append 打开文件并写入但我添加到项目中的 text.txt 文件中的数据没有改变

Thank you Kennyzx!谢谢肯尼兹! I have checked.我检查过。 I really have file text there, but I also i have another text file at [nameproject]\\ that i added into project before didn't change我真的在那里有文件文本,但我也在 [nameproject]\\ 有另一个文本文件,我之前添加到项目中没有改变

Try providing the complete file path and file name when specifying the file to open.在指定要打开的文件时,尝试提供完整的文件路径和文件名。 Rather than "text.txt" use "C:\\YourProjectPath\\YourTextFolder\\text.txt"而不是“text.txt”使用“C:\\YourProjectPath\\YourTextFolder\\text.txt”

Select the text file in your project.选择项目中的文本文件。 Then look in the property window (F4 will show it if you don't have it docked anywhere) for 'Copy to output Directory' and change it from 'Do not copy' to either 'Copy always' or 'Copy if newer'.然后查看属性窗口(如果您没有将它停靠在任何地方,F4 将显示它)“复制到输出目录”并将其从“不复制”更改为“始终复制”或“如果更新则复制”。

After doing this you can read the file using just "text.txt".完成此操作后,您可以仅使用“text.txt”读取文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM