2017-10-16 137 views
0

我想知道是否有人能够帮助我解决这个问题,我搜索了整个互联网,我似乎找不到任何解决方案,我想从字符中读取字节txt文件,首先我使用字符串数组获取以.txt结尾的文件,然后将字符串数组转换为字符串,并使用该字符串读取所有字节并将其放入字节数组中。但是当我运行这个程序时,会出现一个例外,说明System.NotSupportedException。谁能帮忙?先谢谢了。当从字符串读取一个字节时,C#System.NotSupportedException

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(); 
    } 
+1

如果你不打算超过1个文本文件在文件变量中获取单个文件名称,但名称无效。 – BugFinder

+0

OT你确定要从* txt *文件中读取* bytes *吗?还有一个'File.ReadAllText'或'File.ReadAllLines'。 –

回答

1

你得到一个文件数组 - 意味着你得到多个文件。

的代码应该是:

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]); 
} 
+0

它的工作!非常感谢! –