File.Create() 方法在部分路径不存在时失败
File.Create() method fails when part of path does not exist
作为项目练习的一部分,我一直在编写一个简单的控制台应用程序。任务相当简单:
第二种方法必须创建嵌套目录树,其中每个文件夹名称都是 Guid。
第三种方法必须将空文件放在特定级别的选定目录树中。
我的主要问题在于第三种方法。因为虽然它工作正常并创建文件直到任何目录的第三级,但超出该点它总是抛出 "System.IO.DirectoryNotFoundException" - 因为它 "can't find part of the path".
我使用字符串作为路径的容器,但由于它很少有 Guid 集在一起,所以它变得很长。我在创建目录时遇到了类似的问题,但为了工作,我不得不简单地将 @"\?\" 前缀放在路径后面。那么有什么方法可以让它发挥作用,或者绕过它吗?
以下是失败的方法。具体来说是
File.Create(PathToFile + @"\blank.txt").Dispose();
以及生成字符串并调用它的部分代码:
string ChosenDirectoryPath = currDir.FullName + @"\";
for (int i = 0; i <= Position; i++)
{
ChosenDirectoryPath += ListsList[WhichList][i];
}
if (!File.Exists(ChosenDirectoryPath + @"\blank.txt"))
{
FileMaker(ref ChosenDirectoryPath);
}
编辑:
具体来说,目录的制作方法是:
public List<string> DirectoryList = new List<string>();
internal static List<List<string>> ListsList = new List<List<string>>();
private static DirectoryInfo currDir = new DirectoryInfo(".");
private string FolderName;
private static string DirectoryPath;
public void DeepDive(List<string> DirectoryList, int countdown)
{
FolderName = GuidMaker();
DirectoryList.Add(FolderName + @"\");
if (countdown <= 1)
{
foreach (string element in DirectoryList)
{
DirectoryPath += element;
}
Directory.CreateDirectory(@"\?\" + currDir.FullName + @"\" + DirectoryPath);
Console.WriteLine("Folders were nested at directory {0} under folder {1}\n", currDir.FullName, DirectoryList[0]);
ListsList.Add(DirectoryList);
DirectoryPath = null;
return;
}
DeepDive(DirectoryList, countdown-1);
}
由于递归,这很混乱(迭代会更好,但我想这样做是为了学习一些东西)。关键是目录被创建并存储在列表列表中。
创建文件工作正常,但仅适用于前三个嵌套文件夹。所以问题是它以某种方式丢失了它在第 4 级和第 5 级 中的文件路径,甚至无法手动创建它们。会不会是路径太长了?以及如何解决这个问题。
这是抛出的异常:
System.IO.DirectoryNotFoundException: „Can't find part of the path
„C:\Some\More\Foldersb0c7715-ee01-4df8-9079-82ea7990030f\c6c806b0-b69d-4a3a-88d0-1bd8a0e31eb271f2b3-3041-42d5-b631-4719d36c2ac506f00f-7750-4b5a-a45d-cebcecb0b70e\bcacef2b-e391-4799-b84e-f2bc55605d40\blank.txt”.”
所以它抛出了文件的完整路径,但仍然说找不到它。
你的问题是 File.Create
没有为你创建相应的目录,而是抛出一个 System.IO.DirectoryNotFoundException
.
您必须自己创建这些目录,使用 System.IO.Directory.CreateDirectory()
如果由于路径太长而出现此异常,您仍然可以像创建目录时那样使用 "long path syntax (\?\)"。
另请参阅此问题:How to deal with files with a name longer than 259 characters? there is also a good article 已链接
作为项目练习的一部分,我一直在编写一个简单的控制台应用程序。任务相当简单:
第二种方法必须创建嵌套目录树,其中每个文件夹名称都是 Guid。
第三种方法必须将空文件放在特定级别的选定目录树中。
我的主要问题在于第三种方法。因为虽然它工作正常并创建文件直到任何目录的第三级,但超出该点它总是抛出 "System.IO.DirectoryNotFoundException" - 因为它 "can't find part of the path".
我使用字符串作为路径的容器,但由于它很少有 Guid 集在一起,所以它变得很长。我在创建目录时遇到了类似的问题,但为了工作,我不得不简单地将 @"\?\" 前缀放在路径后面。那么有什么方法可以让它发挥作用,或者绕过它吗?
以下是失败的方法。具体来说是
File.Create(PathToFile + @"\blank.txt").Dispose();
以及生成字符串并调用它的部分代码:
string ChosenDirectoryPath = currDir.FullName + @"\";
for (int i = 0; i <= Position; i++)
{
ChosenDirectoryPath += ListsList[WhichList][i];
}
if (!File.Exists(ChosenDirectoryPath + @"\blank.txt"))
{
FileMaker(ref ChosenDirectoryPath);
}
编辑:
具体来说,目录的制作方法是:
public List<string> DirectoryList = new List<string>();
internal static List<List<string>> ListsList = new List<List<string>>();
private static DirectoryInfo currDir = new DirectoryInfo(".");
private string FolderName;
private static string DirectoryPath;
public void DeepDive(List<string> DirectoryList, int countdown)
{
FolderName = GuidMaker();
DirectoryList.Add(FolderName + @"\");
if (countdown <= 1)
{
foreach (string element in DirectoryList)
{
DirectoryPath += element;
}
Directory.CreateDirectory(@"\?\" + currDir.FullName + @"\" + DirectoryPath);
Console.WriteLine("Folders were nested at directory {0} under folder {1}\n", currDir.FullName, DirectoryList[0]);
ListsList.Add(DirectoryList);
DirectoryPath = null;
return;
}
DeepDive(DirectoryList, countdown-1);
}
由于递归,这很混乱(迭代会更好,但我想这样做是为了学习一些东西)。关键是目录被创建并存储在列表列表中。
创建文件工作正常,但仅适用于前三个嵌套文件夹。所以问题是它以某种方式丢失了它在第 4 级和第 5 级 中的文件路径,甚至无法手动创建它们。会不会是路径太长了?以及如何解决这个问题。
这是抛出的异常:
System.IO.DirectoryNotFoundException: „Can't find part of the path „C:\Some\More\Foldersb0c7715-ee01-4df8-9079-82ea7990030f\c6c806b0-b69d-4a3a-88d0-1bd8a0e31eb271f2b3-3041-42d5-b631-4719d36c2ac506f00f-7750-4b5a-a45d-cebcecb0b70e\bcacef2b-e391-4799-b84e-f2bc55605d40\blank.txt”.”
所以它抛出了文件的完整路径,但仍然说找不到它。
你的问题是 File.Create
没有为你创建相应的目录,而是抛出一个 System.IO.DirectoryNotFoundException
.
您必须自己创建这些目录,使用 System.IO.Directory.CreateDirectory()
如果由于路径太长而出现此异常,您仍然可以像创建目录时那样使用 "long path syntax (\?\)"。
另请参阅此问题:How to deal with files with a name longer than 259 characters? there is also a good article 已链接