Click here to Skip to main content
15,893,368 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone,

I have a problem with saving. I need to images which path's are longer than the 260 characters (Sadly they have to be this long. Not my idea!)

I found this one here:
[^]

I tried the \\?\ but it didn't work.

My path is: "\\?\D:\Temp1\Data\"
In this case Visual Studio says that there is an unknown escape sequence.

Then i tried @"\\?\D:\Temp1\Data"
Visual Studio says that there is a sign which is not allowed. I guess it is the "?"

And when I try it without the \\?\ there is the windows exception which says that the Path only allows 260 characters.

What mistake/s did i do?

I hope you can help me.

Best regards
Richard

What I have tried:

These links:
https://blogs.msdn.microsoft.com/bclteam/2007/03/26/long-paths-in-net-part-2-of-3-long-path-workarounds-kim-hamilton/

https://msdn.microsoft.com/en-us/library/aa365247.aspx
Posted
Updated 27-Jun-16 5:57am
Comments
[no name] 27-Jun-16 7:22am    
"\\?\" means computer name. Example : \\server1\publicshare
Example : File.Delete(@"D:\Temp1\Data\file1.txt"):
Evosoul04 27-Jun-16 7:32am    
In the MSDN example it is said, that I should use the \\?\ when I have a path which is longer than 260 characters. Or am I mistaken here? https://msdn.microsoft.com/en-us/library/aa365247.aspx#maxpath
Dave Kreskowiak 27-Jun-16 8:33am    
If you're trying to use the classes and methods built into the .NET Framework System.Io namespace they're not going to work with longer than normal filepaths.

You have to do what you linked to and write you're own methods to handle the basic file operations.

Without seeing your code it's impossible to tell you what you did wrong.
johannesnestler 27-Jun-16 10:24am    
I tested the exact Code from the blog example OP linked - it didn't work, even with the PInvoke calls instead of System.IO...
Dave Kreskowiak 27-Jun-16 11:01am    
I can't test it for you right now. I don't have VS installed yet from a system rebuild and I don't have the time.

Hi Evosoul04,

Sadly I can offer no real help for your problem. But at least I tried to follow the steps from your linked blog article. I copied the code to a little test project. But I observed the same behavior like you - it didn't work (System can not find the path specified), But I think it did everything correct according to: Naming Files, Paths, and Namespaces (Windows)[^]
So at least you can say: others had the same Problem ;) - It seems to me that we miss some important information here to get this working (or Microsoft changed the behavior in between -who knows?)

Greetings from Austria
Johannes
 
Share this answer
 
Comments
Richard MacCutchan 27-Jun-16 11:58am    
See my solution below.
I used the code in the link you provided and it worked as expected. Please edit your question and show the exact code you are using so we can see what is different.

[edit]
The following code works:
C#
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool DeleteFile(string lpFileName);

public static void Delete(string fileName)
{
    string formattedName = @"\\?\D:\Temp\" + fileName;
    DeleteFile(formattedName);
}

[/edit]
 
Share this answer
 
v2
Comments
[no name] 27-Jun-16 18:12pm    
Also if path is more than 260 chars? Only a question...
Richard MacCutchan 28-Jun-16 4:05am    
Don't have any paths that long to test.
Evosoul04 28-Jun-16 1:50am    
Thanks this worked. BUt i found a "better" solution in my opinion. I use the "Zeta Long Path NuGet package". It also imports the kernel32.dll.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900