如何檢查String
值是文件夾還是文件?我使用FileSystemWatcher
以檢查文件的變化或文件夾,然後我這個代碼將其存儲在List(Of String)
:檢查字符串是文件還是文件夾VB.NET
Private Sub logrename(ByVal source As Object, ByVal e As System.IO.RenamedEventArgs)
oldfldrnme.Add(e.OldName)
newfldrnme.Add(e.Name)
End Sub
使用此代碼:
Dim dest As String = Label6.Text
For i = 0 To oldfldrnme.Count - 1 And newfldrnme.Count - 1
Dim attri As FileAttribute = File.GetAttributes(i)
If ((attri And FileAttribute.Directory) = FileAttribute.Directory) Then
Microsoft.VisualBasic.FileIO.FileSystem.RenameDirectory(dest & "\" & oldfldrnme(i), newfldrnme(i))
Else
Microsoft.VisualBasic.FileIO.FileSystem.RenameFile(dest & "\" & oldfldrnme(i), newfldrnme(i))
End If
Next
FileNotFound Exception
拋出,這條線Visual Studio 2010\Projects\File Sync2\File Sync\bin\Debug\0'.
但此代碼:
For i = 0 To oldfldrnme.Count - 1 And newfldrnme.Count - 1
Dim ex As String = Path.HasExtension(i)
If ex = False Then
Microsoft.VisualBasic.FileIO.FileSystem.RenameDirectory(dest & "\" & oldfldrnme(i), newfldrnme(i))
Else
Microsoft.VisualBasic.FileIO.FileSystem.RenameFile(dest & "\" & oldfldrnme(i), newfldrnme(i))
End If
Next
使用目錄的工作,但與文件,它告訴它是一個目錄,扔DirectoryNotFoundException
這一行Could not find directory 'D:\Test2\New Text Document.txt'.
'對於i = 0到oldfldrnme.Count - 1和newfldrnme.Count - 1' - 這是什麼?如果計數是相等的,那麼你會得到這個計數作爲結果,否則我懷疑你實際上是想要他們按位「和」。 – GSerg
[更好的方法來檢查路徑是文件還是目錄?]的可能的重複(http://stackoverflow.com/questions/1395205/better-way-to-check-if-path-is-a-file-或者一個目錄) – GSerg
呃我不明白,但這就是我從'List'獲取文件的方式,它的工作方式只有一行。我的意思是在'RenameDirectory'和'RenameFile'之間選擇,所以我必須使用'IF',但我不知道如何驗證它們。 – Dhan