Coding.Creed
Coding.Creed

Reputation: 13

C# System.NotSupportedException when reading a byte from string

I was wondering if anyone can help me on this one, I've search the whole internet and I can't seem to find any solution, I want to read byte from a txt file, at first i use string array to get the files that end with .txt, follow by converting string array to string and use the string to read all bytes and place it in byte array. But when I run the program it come out an exception stating System.NotSupportedException. Can anyone help?

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

Upvotes: 0

Views: 543

Answers (1)

farbiondriven
farbiondriven

Reputation: 2468

You get an array of files - means you get multiple files.

The code should be:

String[] fileArray = Directory.GetFiles(@"C:\Users\Desktop\feature", "*.txt");
foreach(string file in fileArray){
     Byte[] pFeatureLib = File.ReadAllBytes(file);
}

or if you want only the first file (for any reason):

String[] fileArray = Directory.GetFiles(@"C:\Users\Desktop\feature", "*.txt");
if(fileArray.Length > 0) {
    Byte[] pFeatureLib = File.ReadAllBytes(fileArray[0]);
}

Upvotes: 1

Related Questions