Link to home
Start Free TrialLog in
Avatar of wimthepimscake
wimthepimscake

asked on

Create file with path longer than MAX_LENGTH > 260 chars

I have a problem with writing a file that exceeds the max_length supported by kernel32 funtion CreateFile.

I have managed to write the directory path using this code:

            [DllImport("kernel32.dll", EntryPoint="CreateDirectoryW", SetLastError=true, CharSet=CharSet.Unicode, ExactSpelling=true)]
            private static extern int CreateDirectoryW (string lpPathName, int lpSecurityAttributes);

            private void CreateDirectoryUnicode(string path)
            {
                  string [] pathparts = path.Split('\\');
                  string currentPart = pathparts[0]+"\\"+pathparts[1];
                  int result = CreateDirectoryW(currentPart,0);
                  for (int i=2; i<pathparts.Length; i++)
                  {
                        currentPart += "\\"+pathparts[i];
                        CreateDirectoryW(currentPart,0);
                  }
            }

The question is now, how do I write a file to that path, because my next coide failes because the path is too long:

                        objStreamWriter = new StreamWriter(CacheDirectory + @"\" +
                              htmldoc.ID.ToString() + ".htm", false, System.Text.Encoding.GetEncoding(htmldoc.Encoding));
                        objStreamWriter.Write(htmldoc.Text);
                        objStreamWriter.Close();

Can someone help me?

Wim
ASKER CERTIFIED SOLUTION
Avatar of AlexFM
AlexFM

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Bob Learned
You can extend the length with the '\\?\' prefix.

Bob
Avatar of wimthepimscake
wimthepimscake

ASKER

Bob,

can you please show how this works, I already tried it but I was not able to get it functioning.

Wim
It was worth a shot.  Did you try Alex's solution?

Bob
Yep, I am using Alex's solution now. Thanx for the tips.

Wim
Oke, I cheered to fast, the solution alex gave did not work either.

When I create a file using a streamwriter I am getting the same error.

Wim
Admin, please remove this solution, as it is not correct.
Wim