0

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();
  }
2
  • 1
    If there is more than 1 text file you're not going to get a single file name in your file variable.. but an invalid name. Commented Oct 16, 2017 at 7:19
  • OT are you sure you want to read bytes from a txt file? There is also a File.ReadAllText or File.ReadAllLines. Commented Oct 16, 2017 at 9:24

1 Answer 1

1

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]);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.