473,408 Members | 2,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

GetDirectories throws PathTooLongException

When I pass my directories recursively on a directory structure with
paths 260 chars I get the following exception (see line marked with
***), although I am using only ShortPathNames through Win32API
function::

The specified path, file name, or both are too long. The fully
qualified file name must be less than 260 characters, and the
directory name must be less than 248 characters.

My short path names are < 255 characters, but internally .NET calls
GetFullPathInternal() within Directory.GetDirectories().

I tried to use Unicode-Escape characters @"\\?\" as the following link
suggested, but it did not work:
<http://aspadvice.com/blogs/davidwalk...ve/2007/01/22/
PathTooLongException-work-around.aspx>
Are Unicode paths (which allow us to have paths up to 32.000 chars)
differently escaped in .NET?

Is there another API to get the full path 260 chars?

int CountFilesInDirectory(BackgroundWorker backgroundWorker,
DoWorkEventArgs e, string startDirectoryLongForm, string
startDirectoryShortForm)
{
try
{
if (backgroundWorker != null &&
backgroundWorker.CancellationPending)
{
e.Cancel = true;
return 0;
}

int numFiles = 0;
//*** startDirectoryShortForm = ""D:\\MG0704\\EINSEH~1\\EINSEH~1\
\EINSEH~1\\EINSEH~1\\"";
//*** startDirectoryLongForm = ""D:\\MG0704\\Ein sehr langer
Verzeichnisname 0123456789 0123456789 0123456789 0123456789
0123456789 0123456789 0123456789 0123456789 0123456789 0123456789
0123456789 0123456789Ein sehr langer VerzeichnisnameEin sehr langer
VerzeichnisnameEin sehr langer Verzeichnisname""
foreach (string directoryMixedForm in
Directory.GetDirectories(startDirectoryShortForm)) // *** here the
PathTooLongException is thrown
{
string directoryLongForm = startDirectoryLongForm +
Path.GetFileName(directoryMixedForm);
string directoryShortForm =
PathConverter.ToShortPathName(directoryMixedForm);
numFiles += CountFilesInDirectory(backgroundWorker, e,
directoryLongForm, directoryShortForm);
if (e.Cancel || (backgroundWorker != null &&
backgroundWorker.CancellationPending))
{
return 0;
}
}

int numFilesInThisDirectory =
Directory.GetFiles(startDirectoryShortForm).Length ;
if (numFilesInThisDirectory 0)
{
OnNonEmptyFolderEntered(new
NonEmptyFolderEnteredEventArgs(startDirectoryLongF orm));
if (startDirectoryShortForm.Length >
_longestPathWithoutFileNameShortForm.Length)
{
_longestPathWithoutFileNameShortForm =
startDirectoryShortForm;
}
}

return numFiles + numFilesInThisDirectory;
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.ToString() );
return 0;
}
}

/*
Maximum for path names is 260 (including drive and terminating '\0':
*/
using System;
using System.Text;
using System.Runtime.InteropServices;

namespace ProofPoolCreator
{
/// <summary>
/// Converts file and directory paths to their respective
/// long and short name versions.
/// </summary>
/// <remarks>This class uses InteropServices to call GetLongPathName
and GetShortPathName</remarks>
static class PathConverter
{
[DllImport("kernel32.dll")]
static extern uint GetLongPathName(
string shortname,
[Out] StringBuilder longnamebuff,
uint buffersize);

[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
static extern uint GetShortPathName(
[MarshalAs(UnmanagedType.LPTStr)]
string path,
[MarshalAs(UnmanagedType.LPTStr)]
StringBuilder shortPath,
uint shortPathLength);

/// <summary>
/// The ToShortPathNameToLongPathName function retrieves the long
path form of a specified short input path
/// </summary>
/// <param name="shortName">The short name path</param>
/// <returns>A long name path string</returns>
public static string ToLongPathName(string shortName)
{
StringBuilder longNameBuffer = new StringBuilder(260);
uint bufferSize = (uint) longNameBuffer.Capacity;

GetLongPathName(shortName, longNameBuffer, bufferSize);
string longName = longNameBuffer.ToString();
return AppendPathSeparator(longName);
}

/// <summary>
/// The ToLongPathNameToShortPathName function retrieves the short
path form of a specified long input path
/// </summary>
/// <param name="longName">The long name path</param>
/// <returns>A short name path string</returns>
public static string ToShortPathName(string longName)
{
StringBuilder shortNameBuffer = new StringBuilder(260);
uint bufferSize = (uint) shortNameBuffer.Capacity;

GetShortPathName(longName, shortNameBuffer, bufferSize);
string shortName = shortNameBuffer.ToString();
return AppendPathSeparator(shortName);
}

public static string AppendPathSeparator(string path)
{
if (!path.EndsWith("\\") && !path.EndsWith("/"))
{
return path + '\\';
}
else
{
return path;
}
}
}
}

Jul 16 '07 #1
0 2099

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: MichaelH | last post by:
I have written some code and it was working as expected when I was selecting my USB Drive. I was testing the code and writing the information out to the C:\ drive and the GetDirectories method was...
0
by: Shakil Khan | last post by:
System.IO.PathTooLongException "The path is too long after being fully qualified. Make sure path is less than 260 characters" Can anyone help, How to resolve this issue? This exception is...
2
by: Justin | last post by:
Hi. I'm writing a little c# program to list all the files in a selected directory longer than a given size. The problem is that when it finds a file that is too long for Windows, I get the...
2
by: Mike D | last post by:
Can I control the order that files, folders are displayed? Can I do them alphabetically or do I need to upload to a db like we did in asp? items = Directory.GetDirectories(path) For Each item...
0
by: Carl Rapson | last post by:
I have some code that is checking a directory for subdirectories, using the Directory.GetDirectories method (I got this code from a sample app): Dim directories as String() Try directories =...
2
by: pushpadant | last post by:
I have a code in C# for a console application. Some where in the code I want to get the list of sub-directories inside a particular directory so i use this code string strSubDirs =...
2
by: =?Utf-8?B?ZGdjb29wZXI=?= | last post by:
When I get a list of drives using the Directory.GetLogicalDrives(), it gives me all drives including disconnected network drives. When I attempt to use Directory.GetDirectories() on a disconnected...
2
by: Nathan Sokalski | last post by:
I have an ASP.NET application which displays the directories & files in a specified directory on the server. I use the System.IO.Directory.GetDirectories() and System.IO.Directory.GetFiles() to...
5
by: Gillard | last post by:
hi , i try to io.file.delete(startmenushortcut) i get PathTooLongException how to delete those dead links in my start menu please
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.