Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Create file with path longer than MAX_LENGTH > 260 chars

Posted on 2006-03-31
7
Medium Priority
?
809 Views
Last Modified: 2008-02-01
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
0
Comment
Question by:wimthepimscake
  • 4
  • 2
7 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 2000 total points
ID: 16344155
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16346969
You can extend the length with the '\\?\' prefix.

Bob
0
 
LVL 6

Author Comment

by:wimthepimscake
ID: 16358464
Bob,

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

Wim
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 96

Expert Comment

by:Bob Learned
ID: 16360174
It was worth a shot.  Did you try Alex's solution?

Bob
0
 
LVL 6

Author Comment

by:wimthepimscake
ID: 16360571
Yep, I am using Alex's solution now. Thanx for the tips.

Wim
0
 
LVL 6

Author Comment

by:wimthepimscake
ID: 16361220
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
 
LVL 6

Author Comment

by:wimthepimscake
ID: 16361238
Admin, please remove this solution, as it is not correct.
Wim
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Integration Management Part 2
Loops Section Overview
Suggested Courses
Course of the Month21 days, 4 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question