为什么它抛出 DirectoryNotFoundException 即使 Directory.Exists returns 为真?
Why does it throws DirectoryNotFoundException even though Directory.Exists returns true?
所以我正在制作一个非常小的控制台应用程序来组织一些目录,因为我想至少了解一些关于如何在 C# 上处理文件和目录的基本知识,为此我使用了 Directory.Move
,像这样:
Environment.CurrentDirectory = "/";
try
{
string subDirectoryPath = Path.GetFullPath(subDirectoryName);
string newCompletePath = Path.GetFullPath(newPath);
if (Directory.Exists(subDirectoryPath) && !Directory.Exists(newCompletePath))
{
Directory.Move(subDirectoryPath, Path.GetFullPath(newPath));
SuccessMessage("{0}: -> {1}", subDirectoryName, newPath);
}
ErrorMessage($"The path `{subDirectoryPath}` doesn't exists!");
}
catch (DirectoryNotFoundException ex)
{
ErrorMessage(ex.Message);
ErrorMessage(ex.StackTrace);
Errors.Add($"ERROR ON: {subDirectoryName}");
}
当它到达 de if
语句时,奇怪的事情发生了,它 returns 为真,但当它到达 Directory.Move
函数时,它抛出 DirectoryNotFoundException
。变量 subDirectoryName
是通过 Directory.GetDirectories
获得的字符串之一。我什至试过像这样使用 DirectoryInfo
:
Environment.CurrentDirectory = "/";
try
{
DirectoryInfo directoryInfo = new DirectoryInfo(subDirectoryName);
directoryInfo.MoveTo(newPath);
}
catch (DirectoryNotFoundException ex)
{
ErrorMessage(ex.Message);
ErrorMessage(ex.StackTrace);
Errors.Add($"ERROR ON: {subDirectoryName}");
}
但是还是没有成功,抛出的异常还是一样
我在 Mac 上,对 Mac 使用 Visual Studio,没有任何 运行 异步影响该路径和 GoTo 快捷方式在 Finder 上与 subDirectoryPath
.
的值配合得非常好
我怀疑它与目录的名称有关,因为它们可能有一些奇怪的字符,例如这是其中之一:
/Users/elrohir/Downloads/Comics/[Gokusaishiki (Aya Shachou)] Chikokuma Renko 2 ~Fukushuu no Chikokusha~
(奇怪的字符是 ~)。我尝试使用更简单的名称,如:/Users/elrohir/Downloads/Comics/Comic Name 1
,它在那里工作得很好。关于如何解决这个问题或导致它的原因有什么想法吗?
已修复!改用 documentation 中的这段代码(仍然不知道为什么它以前不起作用)。
Environment.CurrentDirectory = "/";
try
{
DirectoryInfo directoryInfo = new DirectoryInfo(subDirectoryName);
if (!Directory.Exists(newPath))
{
Directory.CreateDirectory(newPath);
}
foreach(FileInfo image in directoryInfo.GetFiles())
{
image.CopyTo(Path.Combine(newPath, image.Name), true);
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.WriteLine(@"Copying {0}\{1}", newPath, image.Name);
Console.ForegroundColor = ConsoleColor.White;
}
SuccessMessage("{0}: -> {1}", subDirectoryName, newPath);
}
catch (DirectoryNotFoundException ex)
{
ErrorMessage(ex.Message);
ErrorMessage(ex.StackTrace);
Errors.Add($"ERROR ON: {subDirectoryName}");
}
现在 subDirectoryName
中的所有 images/files 都被复制到 newPath
。
所以我正在制作一个非常小的控制台应用程序来组织一些目录,因为我想至少了解一些关于如何在 C# 上处理文件和目录的基本知识,为此我使用了 Directory.Move
,像这样:
Environment.CurrentDirectory = "/";
try
{
string subDirectoryPath = Path.GetFullPath(subDirectoryName);
string newCompletePath = Path.GetFullPath(newPath);
if (Directory.Exists(subDirectoryPath) && !Directory.Exists(newCompletePath))
{
Directory.Move(subDirectoryPath, Path.GetFullPath(newPath));
SuccessMessage("{0}: -> {1}", subDirectoryName, newPath);
}
ErrorMessage($"The path `{subDirectoryPath}` doesn't exists!");
}
catch (DirectoryNotFoundException ex)
{
ErrorMessage(ex.Message);
ErrorMessage(ex.StackTrace);
Errors.Add($"ERROR ON: {subDirectoryName}");
}
当它到达 de if
语句时,奇怪的事情发生了,它 returns 为真,但当它到达 Directory.Move
函数时,它抛出 DirectoryNotFoundException
。变量 subDirectoryName
是通过 Directory.GetDirectories
获得的字符串之一。我什至试过像这样使用 DirectoryInfo
:
Environment.CurrentDirectory = "/";
try
{
DirectoryInfo directoryInfo = new DirectoryInfo(subDirectoryName);
directoryInfo.MoveTo(newPath);
}
catch (DirectoryNotFoundException ex)
{
ErrorMessage(ex.Message);
ErrorMessage(ex.StackTrace);
Errors.Add($"ERROR ON: {subDirectoryName}");
}
但是还是没有成功,抛出的异常还是一样
我在 Mac 上,对 Mac 使用 Visual Studio,没有任何 运行 异步影响该路径和 GoTo 快捷方式在 Finder 上与 subDirectoryPath
.
我怀疑它与目录的名称有关,因为它们可能有一些奇怪的字符,例如这是其中之一:
/Users/elrohir/Downloads/Comics/[Gokusaishiki (Aya Shachou)] Chikokuma Renko 2 ~Fukushuu no Chikokusha~
(奇怪的字符是 ~)。我尝试使用更简单的名称,如:/Users/elrohir/Downloads/Comics/Comic Name 1
,它在那里工作得很好。关于如何解决这个问题或导致它的原因有什么想法吗?
已修复!改用 documentation 中的这段代码(仍然不知道为什么它以前不起作用)。
Environment.CurrentDirectory = "/";
try
{
DirectoryInfo directoryInfo = new DirectoryInfo(subDirectoryName);
if (!Directory.Exists(newPath))
{
Directory.CreateDirectory(newPath);
}
foreach(FileInfo image in directoryInfo.GetFiles())
{
image.CopyTo(Path.Combine(newPath, image.Name), true);
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.WriteLine(@"Copying {0}\{1}", newPath, image.Name);
Console.ForegroundColor = ConsoleColor.White;
}
SuccessMessage("{0}: -> {1}", subDirectoryName, newPath);
}
catch (DirectoryNotFoundException ex)
{
ErrorMessage(ex.Message);
ErrorMessage(ex.StackTrace);
Errors.Add($"ERROR ON: {subDirectoryName}");
}
现在 subDirectoryName
中的所有 images/files 都被复制到 newPath
。