Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trailing Space in Directory Name Issue With DirectoryInfo.GetFiles

Status
Not open for further replies.

slider1973

Programmer
Jul 14, 2009
4
US
I have a mapped drive to a Linux mount and need to search for files. One of the directories has a trailing space in the name and this produces the DirectoryNotFoundException error when using the DirectoryInfo.GetFiles method. It seems the method trims the trailing space by default. I am hoping someone has found a workaround for this issue. I'm using C# 2005. The error also happens when using the GetFileSystemInfos method.
 
can you please post the code and the string that represents the directory?
 
The code is shown below. Please note that the error occurs on the line "ListDirectoriesAndFiles(dInfo.GetFileSystemInfos());". The directory string is "Z:\incoming\foldernamewithspace ".

<code>
using System;
using System.IO;

class DirectoryIssue
{
static void Main()
{
try
{
string directory = "Z:/incoming";
DirectoryInfo dir = new DirectoryInfo(directory);
FileSystemInfo[] infos = dir.GetFileSystemInfos();
ListDirectoriesAndFiles(infos);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}

static void ListDirectoriesAndFiles(FileSystemInfo[] FSInfo)
{
foreach (FileSystemInfo i in FSInfo)
{
if (i is DirectoryInfo)
{
DirectoryInfo dInfo = (DirectoryInfo)i;
ListDirectoriesAndFiles(dInfo.GetFileSystemInfos());
}
else if (i is FileInfo)
{
Console.WriteLine("file found");
}
}
}
}
</code>
 
its very interesting that the folder dir has a trailing space since widows will not allow that. Are you sure it is an actually space and not some other special character?
 
The directory is on a Linux server. Linux allows trailing spaces in the directory name. I have a mapped drive on a Windows server to the Linux server. Windows recognizes the trailing space in the mapped drive directory name and C# recognizes the trailing space when listing the directories from the parent level. The error occurs when C# tries to list the contents of the directory with the trailing space.

In order to recreate this issue you must have access to a Linux or Unix server, create a directory there with a traing space, set up the server to allow mapping from a Windows machine, and then map a drive to it from the Windows machine.

You may also be able to do this all on a Windows machine by installing a linux partition but I have not tried that.

I hope this clears up the issue at hand.
 
As a complete aside - why would you want a directory with a trailing space in the name?
 
Good point. The trailing space is caused by another process and really needs to be corrected there. I'm going to treat these as exceptions and not process them if there is no solution to the problem.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top