David Brewer
David Brewer

Reputation: 1974

Copying files network drive getting around 260 character limit C#

Right now i'm attempting to take a screenshot and save it to the local machine, then later copy it to a network drive:

Q:\1234567890123456789012345\123456789012\12345\Screenshots\sc.jpg

^ for an idea about how many characters were talking. I have it setup to create a screenshots folder after that "12345" When the program gets to the point this error occurs:

enter image description here

How can I avoid this?

Also:

DirectoryInfo scdestinfo = new DirectoryInfo(scdest);
DirectoryInfo scinfo = new DirectoryInfo(sccpath);
CopyAll(scinfo, scdestinfo);

That's my code for copying the folders/files.

public void CopyAll(DirectoryInfo source, DirectoryInfo target)
    {
        copyall = false;
        try
        {
            //check if the target directory exists
            if (Directory.Exists(target.FullName) == false)
            {
                Directory.CreateDirectory(target.FullName);
            }//end if
            //copy all the files into the new directory
            foreach (FileInfo fi in source.GetFiles())
            {
                fi.CopyTo(Path.Combine(target.ToString(), fi.Name), true);
            }//end foreach
            //copy all the sub directories using recursion
            foreach (DirectoryInfo diSourceDir in source.GetDirectories())
            {
                DirectoryInfo nextTargetDir = target.CreateSubdirectory(diSourceDir.Name);
                CopyAll(diSourceDir, nextTargetDir);
            }//end foreach
            //success here
            copyall = true;    
        }//end try
        catch (IOException ie)
        {
            MessageBox.Show(ie.Message);
            //handle it here
            copyall = false;
        }//end catch
    }//end CopyAll

And the copyall function.

Upvotes: 1

Views: 1686

Answers (2)

Tigran
Tigran

Reputation: 62256

There is no straightforward way to avoid this, unfortunately.

Solution1: most natural one: generate a path in a way that it doesn't go over that limit.(shorter directory and file names) , or just reorganize directories layout, if this is possible.

Solution2: Don't like this one (wired, imo), but can use WindowsAPI, that doesn't fail in that case, like from the .NET 2.0 Workaround for PathTooLongException (it's for 2.0, but valid for nowdays)

Solution3. Try, when you're going to read/write data to a file, move OS cursor to that directory, so you will not need to specify complete (260+) to access it, but just file name. You should try this to see if it works for you. (even if this works I would prefer the first solution, but it worths to try)

Hope this helps.

Upvotes: 2

Eric J.
Eric J.

Reputation: 150108

This is a Windows limitation, though there are Unicode variants of many API functions that allow for a maximum path length of approximately 32,767 characters, though that is again limited by the specific file system you are dealing with. Typically a given path component is limited to 255 characters (check GetVolumeInformation for the specific limitation for a given volume).

To use the Unicode variants (e.g. CreateFileW) that allow for longer paths, I believe you will have to deal with the Windows API directly rather than using the .Net library functions.

Upvotes: 2

Related Questions