-2

I am trying to read a long characters string using byte[] variable but it is giving me the following error:

{System.IO.PathTooLongException: The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters.

Can anyone please help, thanks.

Please view the following code below. At second line I am getting the above error.

string filename = "Ak8AdFf/gICvXL9Ls3fDVMlLk7WbnddtL6GsobiBzXVLs4SurY6vhsOuMIRduX+PYmY1TYRmh026PbZlw0TNU711pLXGdc1qNqusr7if2WreciqKMJixqceH5WgrcTR2O5NAuXa/l8apkcSd3VQ/dVm3YJ7HxNSp1LvXi+CFWoR5nlNpeIffueCo6Kl1b4JxgGWSeqiBj1qbSKVQs0KoU7tDxUetbDodIai69vd8jSAIIKCEKvXL6BBBBBBBBBBBBBBBBBBBAAAAAAAAAAAAAACCCAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==";
byte[] bmpbyteArray = System.IO.File.ReadAllBytes(filename);
Alex Cohen
  • 5,596
  • 16
  • 54
  • 104
Rauf Abid
  • 313
  • 2
  • 8
  • 24
  • 1
    Possible duplicate of [Best way to resolve file path too long exception](http://stackoverflow.com/questions/8745215/best-way-to-resolve-file-path-too-long-exception) – SHIVANG RANA Jun 16 '16 at 04:31
  • @un-lucky Looks like an ssh key, so would be the content of a file - strange only the end, however, as if someone manually added extra characters. – Aconcagua Jun 16 '16 at 04:40
  • Before you ask a question should stop and think. What exactly does fileName mean? In English words? It means the name of the file of course. You could have avoided asking this embarrassing question just by thinking about it. Don't be so quick to ask a question, try to solve it for yourself before asking. – JK. Jun 16 '16 at 04:41

2 Answers2

2

You are misunderstanding the parameter for the File.ReadAllBytes() method. As clearly stated in the documentation, the signature is as follows:

public static byte[] ReadAllBytes(
    string path
)

The parameter is the path TO the binary file, not the binary file itself. And as such, the filename cannot exceed a specific character length.

David L
  • 32,885
  • 8
  • 62
  • 93
1

If you are not reading a file, you do not need to use the System.IO.File class. It appears that you are just trying to get a byte array of a string. You need to use an Encoding class such as System.Text.Encoding

var bts = System.Text.Encoding.ASCII.GetBytes(filename.ToCharArray());
Mikanikal
  • 1,082
  • 8
  • 12