我有一个如下所示的文件夹名称:
\\server\pictures\607\27022015\HARD\ //temp dir
\\server\pictures\607\27022015\HARD\550528 //dir name (two spaces behind)
macId //a string representative of a folder name (two spaces behind)
在我的代码中,我通过执行以下操作获取文件夹名称:
if (Directory.Exists(tempDirectoryWithoutMac))
{
var subDirectories = Directory.GetDirectories(tempDirectoryWithoutMac);
var directoryToSearch = subDirectories.Where(c => c.Contains(macId.Trim()));
if (directoryToSearch.Any())
{
var newDirectory = directoryToSearch.First();
foreach (string filename in Directory.GetFiles(newDirectory))
{
var retVal = new BitmapImage(new Uri(filename.TrimEnd()));
return retVal;
}
}
}
如果我检查然后将newDirectory
复制并粘贴到Windows资源管理器中,它会将我带到该文件夹;但是,我的代码抛出异常:
The network path was not found.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileSystemEnumerableIterator`1.CommonInit()
at System.IO.FileSystemEnumerableIterator`1..ctor(String path, String originalUserPath, String searchPattern, SearchOption searchOption, SearchResultHandler`1 resultHandler, Boolean checkHost)
at System.IO.Directory.GetFiles(String path)
at URM.ViewModels.CollectionsViewModel.GetImages(String macId, String type)
如果我尝试重命名文件夹,取出末尾的空格...... Windows说......源文件名和目标文件名是相同的。
修改 我正在粘贴这条路径(用于显示空格的引号):
“\\服务器\图片\ 607 \ 27022015 \ HARD \ 550528”
答案 0 :(得分:2)
如果您使用DirectoryInfo和FileInfo对象作为常规字符串,那么您将需要处理更少的空白问题,并能够带来更多面向对象和可靠的解决方案。
您可以执行以下操作:
DirectoryInfo tempWithoutMac = new DirectoryInfo(tempDirectoryWithoutMac);
DirectoryInfo MacID = new DirectoryInfo(macId);
DirectoryInfo wantedDir = tempWithoutMac.GetDirectories.Where(c => c.Contains(MacID)).First();
foreach (FileInfo file in wantedDir.GetFiles())
{
var retVal = new BitmapImage(new Uri(file.fullname));
return retVal;
}
这只是一个示例,因此您需要调整它以便为您工作,但使用此概念可以帮助您。
干杯,
答案 1 :(得分:1)
可能会使用alt code在alt + 0160
的文件系统条目(文件/文件夹)末尾添加出现的字符,特别是{ {3}}:
使用此技巧,您可以显示没有名称的文件夹。
...
如果右键单击该文件夹,请选择“重命名”并仅输入空格,操作系统将不接受该文件夹。
要删除名称并显示空白名称,请右键单击该文件夹,然后选择“重命名”。现在按Alt键,然后按数字键盘,按0160。
文件系统将不视为'正常'空格( alt + 0032
),但作为' 没有休息空间'。另请参阅answers.microsoft.com上的此链接:" how do I add a space in front of a folder name?" (提问者想要文件夹名称开始的空格,因此它首先出现在字母表中:)
实际上,没有休息空间"是ALT + 0160 ......
Windows资源管理器中的解决方案
我认为如果你重命名文件夹,即从"550528 "
到临时名称(例如"temp"
),那么你就可以将它重命名为"550528"
(注意缺少空间)成功。
代码中的解决方案
正如其他人所说,如果您在包含文件夹中enumerate through the file system entries,那么您最终会在没有考虑的情况下获得带有正确特殊字符的条目。
答案 2 :(得分:0)
Windows资源管理器删除目录输入末尾的多余空格。例如,尝试在某处创建一个名为“foo”的文件夹,然后尝试创建一个“foo”文件夹(末尾带有空格)。 Windows会抱怨已经有一个包含该名称的文件夹。仅仅因为资源管理器程序接受它并不能使它正确。使用空格制作不同的文件夹可能不是最好的主意。