假设我正在使用File.WriteAllText将非常长的字符串写入文件,而另一个线程或进程正在尝试读取同一个文件.会抛出任何异常吗?换句话说,File.WriteAllText方法使用的FileShare参数是什么?它没有写在文档中!
这是将字符串写入文件的代码
System.IO.File.WriteAllText("test.txt", "P ");
Run Code Online (Sandbox Code Playgroud)
它基本上是字符"P",后跟总共513个空格字符.
当我在Notepad ++中打开文件时,它看起来没问题.但是,当我在Windows记事本中打开时,我看到的只是乱码.
如果不是513空格字符,我添加514或512,它在记事本中打开正常.
我错过了什么?
File.WriteAllText在每个字母和引号后插入一个空格.
例:
原始文件
"JobID" "ParentJobID"
Run Code Online (Sandbox Code Playgroud)
新文件
" J o b I D " " P a r e n t J o b I D "
Run Code Online (Sandbox Code Playgroud)
码
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ProcessOutputLogTransfer
{
class Program
{
static void Main(string[] args)
{
string content = File.ReadAllText(@"C:\Documents and Settings\All Users\Application Data\Microsoft\Windows NT\MSFax\ActivityLog\OutboxLOG.txt");
File.WriteAllText(@"C:\FAXLOG\OutboxLOG.txt", content, Encoding.UTF8);
}
}
}
Run Code Online (Sandbox Code Playgroud) 我现在一直在寻找许多网站上的答案,但所有工作答案仅适用于richTextbox,而我正在使用普通的文本框.我正在尝试将文本框的内容保存到选择的文件中,但由于某种原因文件没有保存,我不知道问题是什么.这是"保存"菜单项的代码:
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFileDialog ofd = new SaveFileDialog();
ofd.Title = "Save";
ofd.Filter = "Txt Documents (.txt)|*.txt|All files (*.*)|*.*";
if (ofd.ShowDialog() == DialogResult.OK)
{
try
{
//I don't know what to make of this, because clearly this doesn't work
File.WriteAllText(@"./TestFile.txt", MainTextbox.Text);
}
catch (Exception ex)
{
MainTextbox.Text += ex;
}
}
}
Run Code Online (Sandbox Code Playgroud)
没有错误.
c# ×4
writealltext ×4
concurrency ×1
file ×1
io ×1
notepad ×1
notepad++ ×1
save ×1
writefile ×1