C# 从字符串中读取字节时,C System.NotSupportedException
我想知道是否有人能在这方面帮助我,我搜索了整个互联网,似乎找不到任何解决方案,我想从txt文件中读取字节,首先我使用字符串数组获取以.txt结尾的文件,然后将字符串数组转换为字符串,并使用字符串读取所有字节并将其放入字节数组中。但是当我运行程序时,它会出现一个异常,声明System.NotSupportedException。有人能帮忙吗?提前谢谢C# 从字符串中读取字节时,C System.NotSupportedException,c#,C#,我想知道是否有人能在这方面帮助我,我搜索了整个互联网,似乎找不到任何解决方案,我想从txt文件中读取字节,首先我使用字符串数组获取以.txt结尾的文件,然后将字符串数组转换为字符串,并使用字符串读取所有字节并将其放入字节数组中。但是当我运行程序时,它会出现一个异常,声明System.NotSupportedException。有人能帮忙吗?提前谢谢 String[] fileArray = Directory.GetFiles(@"C:\Users\Desktop\feature", "*.t
String[] fileArray = Directory.GetFiles(@"C:\Users\Desktop\feature", "*.txt");
String file = ConvertStringArrayToString(fileArray);
Byte[] pFeatureLib = File.ReadAllBytes(file); // error occur here
public String ConvertStringArrayToString(String[] array)
{
// Concatenate all the elements into a StringBuilder.
StringBuilder builder = new StringBuilder();
foreach (string value in array)
{
builder.Append(value);
builder.Append('.');
}
return builder.ToString();
}
你得到一个文件数组-意味着你得到多个文件 代码应为:
String[] fileArray = Directory.GetFiles(@"C:\Users\Desktop\feature", "*.txt");
foreach(string file in fileArray){
Byte[] pFeatureLib = File.ReadAllBytes(file);
}
或者,如果出于任何原因只需要第一个文件:
String[] fileArray = Directory.GetFiles(@"C:\Users\Desktop\feature", "*.txt");
if(fileArray.Length > 0) {
Byte[] pFeatureLib = File.ReadAllBytes(fileArray[0]);
}
如果有多个文本文件,则无法在文件变量中获得单个文件名。。但名称无效。OT是否确实要从txt文件中读取字节?还有一个File.ReadAllText或File.ReadAllLines。