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
LVL 6
wimthepimscakeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AlexFMCommented:
You can use Directory.SetCurrentDirectory Method number of times providing relative path, by the same way as you create directory. When this directory is current, create file using only file name.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bob LearnedCommented:
You can extend the length with the '\\?\' prefix.

Bob
0
wimthepimscakeAuthor Commented:
Bob,

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

Wim
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Bob LearnedCommented:
It was worth a shot.  Did you try Alex's solution?

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

Wim
0
wimthepimscakeAuthor Commented:
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
0
wimthepimscakeAuthor Commented:
Admin, please remove this solution, as it is not correct.
Wim
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.