【问题标题】:Directory.Move isn't trying the correct folderDirectory.Move 没有尝试正确的文件夹
【发布时间】:2013-09-21 12:06:12
【问题描述】:

我编写了一些代码,旨在将我 PC 上的所有电影分类到字母表中每个字母的子文件夹中(例如,“An Example”将进入仅包含以字母“A”开头的电影的子文件夹中。

在我看来,我编写的代码应该可以毫无问题地工作,尽管出于某种原因,这段代码:

'Declarations
System.IO.Directory.CreateDirectory("D:\Vuze Downloads\Movies\.CAMS")
System.IO.Directory.CreateDirectory("D:\Vuze Downloads\Movies\.TS")
Dim TS As String = "D:\Vuze Downloads\Movies\.TS\"
Dim CAM As String = "D:\Vuze Downloads\Movies\.CAMS\"
Dim mainFolder As New System.IO.DirectoryInfo("D:\Vuze Downloads\Movies\")

For Each f As System.IO.DirectoryInfo In mainFolder.GetDirectories()
    If Not UCase(f.ToString).Contains("(CAM)") And UCase(f.ToString).Contains("(TS)") Then
        System.IO.Directory.Move(f.ToString, mainFolder.Name & UCase(Left(f.Name, 1)) & "\" & f.Name)
    ElseIf UCase(f.Name.ToString).Contains("(CAM)") And Not UCase(f.Name.ToString).Contains("(TS)") Then
        System.IO.Directory.Move(f.ToString, CAM.ToString & UCase(Mid(f.Name, 5)) & "\" & f.Name.Substring(5))
    ElseIf UCase(f.Name.ToString).Contains("(TS)") And Not UCase(f.Name.ToString).Contains("(CAM)") Then
        System.IO.Directory.Move(f.ToString, TS.ToString & UCase(Mid(f.Name, 6)) & "\" & f.Name.Substring(6))
    End If
Next

在这一行不断抛出异常:

System.IO.Directory.Move(f.ToString, CAM.ToString & UCase(Mid(f.Name, 5)) & "\" & f.Name.Substring(5))

这是个例外:

An unhandled exception of type 'System.IO.DirectoryNotFoundException' occurred in
 mscorlib.dll

Additional information: Could not find a part of the path 
'D:\Users\Yorrick\documents\visual studio 2012\Projects\filmsort\filmsort\bin\Debug\(CAM)The Internship'.

如上面的声明所示,我不知道这段代码如何或为什么试图访问该文件夹。

如果有人能看一看并希望能发现我所犯的错误,那将不胜感激。

编辑:

解决了上述问题后,我遇到了一个新问题。 使用下面的代码,我现在得到了同样的异常,除了这次附加信息说“找不到路径的一部分”。我的所有变量在调试时似乎都是正确的,所以我真的不明白为什么这不起作用。

注意:注释行是我尝试过的,它给了我System.IO.IOException: Cannot create a file when that file already exists.

代码:

System.IO.Directory.CreateDirectory("D:\Vuze Downloads\Movies\.CAMS")
System.IO.Directory.CreateDirectory("D:\Vuze Downloads\Movies\.TS")
Dim TS As String = "D:\Vuze Downloads\Movies\.TS\"
Dim CAM As String = "D:\Vuze Downloads\Movies\.CAMS\"
Dim mainFolder As New System.IO.DirectoryInfo("D:\Vuze Downloads\Movies\")

For Each f As System.IO.DirectoryInfo In mainFolder.GetDirectories()
    If Not UCase(f.ToString).Contains("(CAM)") And UCase(f.ToString).Contains("(TS)") Then
        System.IO.Directory.Move(f.FullName, mainFolder.FullName & UCase(Left(f.Name, 1)) & "\" & f.Name)
    ElseIf UCase(f.Name.ToString).Contains("(CAM)") And Not UCase(f.Name.ToString).Contains("(TS)") Then
        'System.IO.Directory.CreateDirectory(CAM & UCase(Mid(f.Name, 6)) & "\" & f.Name.Substring(6))
        System.IO.Directory.Move(f.FullName, CAM & UCase(Mid(f.Name, 6)) & "\" & f.Name.Substring(6))
    ElseIf UCase(f.Name.ToString).Contains("(TS)") And Not UCase(f.Name.ToString).Contains("(CAM)") Then
        System.IO.Directory.Move(f.FullName, TS.ToString & UCase(Mid(f.Name, 5)) & "\" & f.Name.Substring(5))
    End If
Next

【问题讨论】:

  • 我注意到的第一件事是UCase 的使用,这确实是一个旧的 vb6 遗留函数,你应该改用.ToUpper.Contains(),虽然那只是吹毛求疵,没有什么对我跳出来的,所以我会尝试重现此问题以找出原因。
  • 好点,我似乎一直在使用 UCase,即使我一直被告知它已经过时了,但是嘿,它具有完全相同的功能,所以我想使用它没有问题,对吧? :P 无论哪种方式,在答案中已经列出了它为什么起作用的原因,解决了这个问题,但现在它找不到路径的一部分。

标签: vb.net directory move .net


【解决方案1】:

当您没有指定完整路径名(例如“c:\foo\bar”)而是指定相对路径名(例如“bar”)时,就会发生这种情况。通过预先添加 Environment.CurrentDirectory 将相对路径名转换为完整路径名。默认情况下是您项目的构建目录。

这是因为您使用了 DirectoryInfo.Name。对于路径为 c:\foo\bar 的目录,它是“bar”。您必须改用 FullName 属性。

【讨论】:

  • 如果我对您的理解正确,您是在建议我使用System.IO.Directory.Move(f.FullName, CAM & UCase(Mid(f.Name, 6)) & "\" & f.Name.Substring(6)),对吗?如果是这样,由于某种原因,这给了我另一个例外,说它找不到路径的一部分(虽然它没有指定哪个路径)
  • 我看到你使用 mainFolder.Name,应该是 mainFolder.FullName。其余的让我的眼睛流血。通过将字符串分配给局部变量来调试它,以便您可以看到它。并且始终支持 Path.Combine() 方法来构建路径字符串。
  • 是的,我还没有编辑它,但它仍然给我刚才提到的错误
【解决方案2】:

我只是重写了一点,我认为这应该足以满足您的目的。

System.IO.Directory.CreateDirectory("D:\Vuze Downloads\Movies\.CAMS")
System.IO.Directory.CreateDirectory("D:\Vuze Downloads\Movies\.TS")
Dim TS As String = "D:\Vuze Downloads\Movies\.TS\"
Dim CAM As String = "D:\Vuze Downloads\Movies\.CAMS\"
Dim mainFolder As New System.IO.DirectoryInfo("D:\Vuze Downloads\Movies\")

For Each f As System.IO.DirectoryInfo In mainFolder.GetDirectories()
    If f.Name.ToUpper.Contains("(TS)") Then
        System.IO.Directory.Move(f.FullName, System.IO.Path.Combine(TS, f.Name))
    ElseIf f.Name.ToUpper.Contains("(CAM)") Then
        System.IO.Directory.Move(f.FullName, System.IO.Path.Combine(CAM, f.Name))
    End If
Next

【讨论】:

    猜你喜欢
    • 2020-06-17
    • 2014-02-27
    • 1970-01-01
    • 2020-04-07
    • 2013-09-30
    • 2011-11-06
    • 1970-01-01
    • 2015-11-11
    • 2018-09-03
    相关资源
    最近更新 更多