Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET General > How to resolve the specified path, file name, or both are too long.


Reply
 
Thread Tools Display Modes
  #1  
Old 08-06-2011, 04:52 PM
Michael___ Michael___ is offline
Centurion
 
Join Date: Sep 2009
Posts: 111
Default How to resolve the specified path, file name, or both are too long.


I am making a program that copies directories from one drive to another. Everything seemed to be going just fine until I tried to copy a file with a long file path. It says that that the file name must be less than 260 characters and the directory name must be less than 248 characters. The problem is that I can't just not copy these files. I seem to be able to copy them fine using the Windows Explorer. How do I make my program like that. I read somewhere about prefixing the file path with "\\?\". But I couldn't get that to work. What can I do to handle very long file paths.
Reply With Quote
  #2  
Old 08-07-2011, 12:46 PM
Michael___ Michael___ is offline
Centurion
 
Join Date: Sep 2009
Posts: 111
Default

Also, sometimes when I try to copy a file, it says: Access Denied. How can I not have access when I am the only user on the computer? This may however be an issue if I put it on someone else's computer. Is there a way of bring that window up in which the administrator enters their password. It appears if you try to install a program and you're not allowed to. That would be great if I could do that.

What would be even better is if I could just bypass the whole issue. Is there such a non-hack way? I doubt it but it's worth a shot.
Reply With Quote
  #3  
Old 08-07-2011, 04:42 PM
xer0syk0 xer0syk0 is offline
Centurion
 
Join Date: Oct 2004
Posts: 145
Default

What problems did you run into when using the CopyFile API? I'm going to assume you tried following the code example from this website: http://blogs.msdn.com/b/bclteam/arch...-hamilton.aspx.

Note: Since it's a kernel function, I'm pretty sure the app will have to run with administrator privileges in order to work correctly.

Which files are you trying to copy when you get the 'Access Denied' error and to where?
Reply With Quote
  #4  
Old 08-07-2011, 05:15 PM
PlausiblyDamp's Avatar
PlausiblyDamp PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 1,887
Default

Without knowing which files are giving the access denied error it is hard to say exactly what the problem is; however there are certain files and folders that are restricted to the OS itself and even administrator accounts don't have permissions by default. If you are running on Vista and later even if you have permissions you may need to run the application elevated (UAC can block access) to be able to access the files in question.
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #5  
Old 08-08-2011, 08:03 AM
Michael___ Michael___ is offline
Centurion
 
Join Date: Sep 2009
Posts: 111
Default

I think I have seen the web page that you gave to link to. But I dismissed it because the code was in C Sharp. And after going on a C Sharp to Basic converter it said there was an error when I tried to convert the code under the heading Finding Files and Directories.
Quote:
-- line 9 col 17: invalid TypeDecl
Looking back now I see that in order to copy the files I would need to do a combination of the codes "Writing to or Reading from a file" and "Finding Files and Directories" in order to copy. I didn't realise this at the time.

You said:
Quote:
Note: Since it's a kernel function, I'm pretty sure the app will have to run with administrator privileges in order to work correctly.
How can I do this. On a different website it said to look under My Project -> Application -> View UAC Settings but I could find the button View UAC Settings. I am using Visual Basic 2010 .NET. What I am after is that box where the administrator must enter there password. A similar box as to the one when you try to install a new program.

The file I am trying to copy is:
C:\Users\Michael\AppData\Local\VirtualStore\Program Files\Adobe\Adobe After Effects CS5\Support Files\Plug-ins\Effects\Synthetic Aperture\(Color Finesse 3 Support)\Color Finesse 3\Color Finesse Presets\35mm Filmstocks\Eastman Kodak\Eastman 5222 Double-X B&W.cfpreset
My application backs up files. So it is trying to copy it to the 'D' drive: The directory path is identical except for the drive.
The directory where the file should be copied to is:
D:\Users\Michael\AppData\Local\VirtualStore\Program Files\Adobe\Adobe After Effects CS5\Support Files\Plug-ins\Effects\Synthetic Aperture\(Color Finesse 3 Support)\Color Finesse 3\Color Finesse Presets\35mm Filmstocks\Eastman Kodak\
Other files in the same directory were copied fine, it is the just the ones with a slightly longer file name.

Finally, I am currently running on Windows 7 with a 64 Bit operating system. how can I globalize my application so that it runs on XP, Vista and 7 regardless of whether or not it's 64 Bit or 32 Bit. Would I just have to recreate my application and change the code slightly?
Reply With Quote
  #6  
Old 08-13-2011, 05:47 AM
Michael___ Michael___ is offline
Centurion
 
Join Date: Sep 2009
Posts: 111
Default

If anyone has an answer I would still really appreciate it.
Reply With Quote
  #7  
Old 08-13-2011, 06:10 AM
PlausiblyDamp's Avatar
PlausiblyDamp PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 1,887
Default

Converting the C# from the other link shouldn't be too difficult, it might help if you posted the actual line(s) giving the error (and related code) - telling us
Quote:
-- line 9 col 17: invalid TypeDecl
doesn't give us much information to help you with.

Alternatively http://blogs.msdn.com/b/msdnmagazine...6/1957449.aspx might be worth a look as well.
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #8  
Old 08-13-2011, 07:18 AM
Michael___ Michael___ is offline
Centurion
 
Join Date: Sep 2009
Posts: 111
Default

Sorry, I wasn't really expecting anyone to convert it. But here's the link again:
http://blogs.msdn.com/b/bclteam/arch...-hamilton.aspx
I've tried a few online C Sharp to VB.NET converters and they all say something to do with "invalid TypeDecl".

Here is the code that I tried:
Code:
using System;
using System.Runtime.InteropServices;
 
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool DeleteFile(string lpFileName);
It says that the error occurred on line 6. This error also happens when trying some other code snippets from that web page.

Also, I looked at that web page and got to this one: http://msdn.microsoft.com/en-gb/magazine/cc163677.aspx

Why is it that something so common is so difficult to do?
Reply With Quote
  #9  
Old 08-13-2011, 10:34 AM
AtmaWeapon's Avatar
AtmaWeapon AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,505
Default

It's not common. You aren't supposed to make paths that require more than ~260 characters to express. You can't create them normally. Most programs don't expect it because Windows says you shouldn't do it. It's hard to work with long file paths for the same reason it's hard to make a motorcycle fly to the moon.

Edit:
Actually, here's a more apt comparison. It's hard to use long file paths for the same reason it's hard to buy crystal meth from the grocery store. You might find some store with a secret knock that lets you in a back room where the drug is sold. It won't be true of all stores, and bad things happen when addicts intermingle with normal people.


You're in this mess because some backup program had the bright idea to save directory structures but also append the AppData path in front. With a sensible username, that eats up roughly 40 characters of your 260. So if you had a path that was already pushing the limits, you're in trouble. I'd like to note the original path of that vile is 267 characters long. We can deduce some things from this:
  • Adobe After Effects supports long file paths.
  • The plugin's installer supports long file paths.
  • The plugin's vendor is stupid.
If I noticed something I installed put files at too long a path, I'd file it as a bug. Our product has an installer, and we've had to spend weeks of effort sometimes deciding how to place things so we don't hit the path limit. We *could* make our installer use the workarounds to save things at long paths, but we have to routinely ask our customers to copy files from the directories that would be affected and our support burden would be huge. Also if you're going to write Windows software and sell it you should learn the rules and play by them.

If you're writing the backup program and it's not for sale, consider telling users with these long files to pound sand and fix their machines. I got to do that for one of my tools and it was quite pleasant. Some guy had a path with 800 characters in it because each time it got too long he made a new mapped drive. So we told him his stuff wasn't going to be backed up and the problem solved itself.

If you can't tell users to punt, consider a solution that doesn't use the "rsync" approach. Many backup programs store files in a virtual filesystem. This could be a binary file with some text file describing its contents or perhaps you might consider using Win7's support for true virtual disks. The easiest way to not have to deal with long file paths is to not create them yourself.

OK, rant over. Let's do stuff. My guess about the "Invalid TypeDecl" error is that this line:
Code:
[return: ...
... is a C# specific PInvoke syntax. I maintain a C# library with 100+ PInvoke calls and I've never used it, so I don't plan on investigating. Generally PInvoke figures out that kind of stuff; the attributes are for when it gets it wrong.

I made a path like this on my system:
Code:
C:\temp\asdfasdf\asdfasdf\asdfasdf\asdfasdf\asdfasdf\asdfasdf\asdfasdf\
    asdfasdf\asdfasdf\asdfasdf\asdfasdf\asdfasdf\asdfasdf\asdfasdf\asdfasdf\asdfasdf\
    asdfasdf\asdfasdf\asdfasdf\asdfasdf\asdfasdf\asdfasdf\asdfasdf\asdfasdf\asdfasdf\
    longlonglonglongfilename.longextension
That's 272 characters if I counted right. Technically I didn't create the file because I didn't want to bother with mapping a path. So I had to start with putting contents in the file. For that, we need the CreateFile() API. I didn't want to bother with conversion, so I used PInvoke.net, something suggested in part 3 of the article and a handy tool for PInvoke.

After a couple of failures where I got my flags mixed up, success! I could go on and try out all the other API methods, but I think between the article, pinvoke.net, and my attached example I think you ought to be able to make some progress. I'm also not super interested because I think the world would be better if people didn't create paths that were longer than Windows allows.

(The example obviously doesn't work as-is because you don't have the directory I created. You can fill in your own long paths to try it out.)

*edit*
Also, pay no heed to "it's a kernel function so you need Administrator permission". Plenty of user-mode functions live in Kernel32.dll. If you needed kernel-mode access to work with files, it'd be impossible to do anything without UAC elevation. You won't generally stumble upon kernel-mode calls in the normal API documentation; that's mainly kept in the driver development docs.
Attached Files
File Type: zip LongFilePaths.zip (9.2 KB, 36 views)
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.

Last edited by AtmaWeapon; 08-13-2011 at 10:47 AM.
Reply With Quote
  #10  
Old 08-13-2011, 11:01 AM
Michael___ Michael___ is offline
Centurion
 
Join Date: Sep 2009
Posts: 111
Default

Thank you very much. This was a massive help for me. I understand what you mean about the long file paths and couldn't agree more.
Reply With Quote
  #11  
Old 08-13-2011, 11:22 AM
PlausiblyDamp's Avatar
PlausiblyDamp PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 1,887
Default

I tried the converter at http://www.developerfusion.com/tools.../csharp-to-vb/ and it gave that error, however if I just pasted in the [dllimport...] lines they converted just fine, oddly enough the remainder of the code converted if you paste it in without the [dllimport...] bits - go figure.
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #12  
Old 05-29-2014, 08:43 AM
alexon alexon is offline
Newcomer
 
Join Date: May 2014
Posts: 1
Default Long Path Files...

Hi try to use Long path tool to fix your problem, Thanks
Reply With Quote
Reply

Tags
directory, file, long, names, paths


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->