![](/img/trans.png)
[英]C# File.ReadAllText returning “NotSupportedException”
[英]C# File.ReadallText doing weird things
我正在尝试做的是读取文件中的所有文本,如果它包含单词“共享”做一个正则表达式。 这是代码:
DirectoryInfo dinfo = new DirectoryInfo(@"C:\Documents and Settings\g\Desktop\123");
FileInfo[] Files = dinfo.GetFiles("*.txt");
foreach (FileInfo filex in Files)
{
string contents = File.ReadAllText(filex.FullName);
string matchingcontants = "Share";
if (contents.Contains(matchingcontants))
{
string sharename = Regex.Match(contents, @"\+(\S*)(.)(.*)(.)").Groups[3].Value;
File.AppendAllText(@"C:\sharename.txt", sharename + @"\r\n");
}
}
当我调试时,我得到...... contents =“\\ r \\ 0 \\ n \\ 0 = \\ 0 = \\ 0 = \\ 0 = \\ 0 = \\ 0 = \\ 0 = \\ 0 = \\ 0 = \\ 0 = \\ 0 = \\ 0 = \\ 0 = \\ 0 = \\ 0 = \\ 0 = \\ 0 = \\ 0 = \\ 0 = \\ 0 = \\ 0 = \\ 0 = \\ 0 = \\ 0 = \\ 0 = \\ 0 = \\ 0 = \\ 0 = \\ 0 = \\ 0 \\ r \\ 0 \\ n \\ 0 + \\ 0S \\ 0h \\ 0a \\ 0r \\ 0e \\ 0 \\ 0 \\\\ 0 \\\\ 0j \\ 05 \\ 02 \\ 0 \\\\ 0w \\ 0w \\ 0w \\ 0_ \\ 0O \\ 0N \\0吨\\
\\ 0S \\ 0H \\ 0A \\ 0R \\ 0E \\
不共享。 任何提示? 提示或建议?
看起来你有一个保存为UTF-16的文件(即Encoding.Unicode
)。 使用正确的编码读取文件,一切都应该很好。
幸运的是,File.ReadAllText的重载采用了编码:
string contents = File.ReadAllText(filex.FullName, Encoding.Unicode);
不幸的是,对于不是 UTF-16的文件,这将是错误的。 虽然存在猜测编码的启发式方法,但理想情况下,您应该在打开文件之前知道编码。
看起来它是一个Unicode文件,并且您尝试将其作为纯ASCII读取。
我的猜测是编码设置不正确,您可能需要使用指定编码的ReadAllText(String,Encoding)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.