Deleting long path too long folder from Network Share
Asked Answered
D

1

7

I'm trying to delete folders in a shared location on a network using C#. Some of the folder paths are too long for Windows to handle. I've tried multiple options for this. The best one I found was creating a FileSystemObject, adding \\?\ to the path and calling DeleteFolder on the path that I want to delete, which works on my local computer for paths that are too long, because I have mapped drives like C: and G: etc, but when I try to use it on a Network share folder I get either a HRESULT: 0x800A004C (CTL_E_PATHNOTFOUND) or value does not fall within the expected range.

The following is my code:

private static void DeletePathWithLongFileNames(string path)
    {
        string tmpPath = @"\\?\" + path;
        FileSystemObject fso = new FileSystemObject();
        fso.DeleteFolder(tmpPath, true);
    }

let's say for example, the network + share folder is \\myServer\mySharedFolder\folder1\etc\etc, which would be the path string I'm sending to my delete function then the tmpPath is showing as "\\\\?\\\\\\myServer\\mySharedFolder\\folder1\\etc\\etc"

I don't know much about UNC so I don't know if this is what is wrong or not. I'm pretty sure there is something wrong with my tmpPath variable, but again I'm not sure. Maybe it's a syntax error But I can't for the life of me figure out what is wrong. Thanks in advance for the help

EDIT: I believe I have found the answer, I am testing it right now. So far it has worked for me. if I run the DeleteFolder method on the following path \\?\UNC\server\sharedFolder\folder1\etc\etc" this seems to work. Now I just have to figure out how to get rid of all those extra slashes.

EDIT 2: This does work, tested it on a Share folder on a network. It just came down to me not understanding UNC paths.

Dexter answered 10/7, 2017 at 14:36 Comment(0)
T
0

The safe way to delete paths that are too long is to use AlphaFS. AlphaFS is a .NET library providing more complete Win32 file system functionality to the .NET platform than the standard System.IO classes. The most notable deficiency of the standard .NET System.IO is the lack of support of advanced NTFS features, most notably extended length path support (eg. file/directory paths longer than 260 characters).

See Directory Delete: http://alphafs.alphaleonis.com/doc/2.2/api/html/BE179564.htm

Alphaleonis.Win32.Filesystem.Directory.Delete(path)
Theism answered 25/2, 2019 at 16:3 Comment(1)
The edit changes this into a completely different answer. I suppose that's mostly fine since there were no votes on it, but the information in the first revision might still prove useful to someone, particularly since the current revision is presenting a GUI application written in C as the solution to a C# question. In my mind, that kind of makes this answer wrong/unhelpful, since it no longer addresses what was asked.Sutler

© 2022 - 2024 — McMap. All rights reserved.