如何检查String
值是文件夹还是文件?我使用FileSystemWatcher
检查文件或文件夹中的更改,然后将其存储在List(Of String)
中并使用以下代码:
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'.
答案 0 :(得分:3)
首先,您无法在For Next
语句中使用Path.HasExtension()
。这会导致您按位组合两个整数。
其次,文件可能没有扩展名,因此检查IO.Path.Combine()
可能会在某个时刻引发异常。
第三,要将路径连接在一起,最好使用Math.Min()
。
首先我们处理按位组合语句。使用IndexOutOfRangeException
,您将获得两个值中最低的值(您必须这样做,因为如果由于某种原因,某个项目的项目数多于另一个,则代码将抛出For i = 0 To Math.Min(oldfldrnme.Count, newfldrnme.Count) - 1
)。
Directory.Exists(i)
现在让我们检查路径是否是文件。您无法执行i
因为Directory.Exists(<path to be renamed>)
是您的Integer变量,而不是完整路径。要正确检查,您必须IO.Path.Combine()
。我们使用For i = 0 To Math.Min(oldfldrnme.Count, newfldrnme.Count) - 1
Dim DestPath As String = Path.Combine(dest, oldfldrnme(i)) 'This is the file/directory to be renamed.
If File.Exists(DestPath) Then
ElseIf Directory.Exists(DestPath) Then
End If
Next
来正确获取目标路径。
For i = 0 To Math.Min(oldfldrnme.Count, newfldrnme.Count) - 1
Dim DestPath As String = Path.Combine(dest, oldfldrnme(i)) 'This is the file/directory to be renamed.
If File.Exists(DestPath) Then
Microsoft.VisualBasic.FileIO.FileSystem.RenameFile(DestPath, newfldrnme(i))
ElseIf Directory.Exists(DestPath) Then
Microsoft.VisualBasic.FileIO.FileSystem.RenameDirectory(DestPath, newfldrnme(i))
End If
Next
最后,我们进行重命名。
Try Catch
修改强>
正如Cody Gray指出的那样,如果其他事情对您的文件/目录执行I / O操作,您应该在代码中添加一些异常处理。
因此我们将围绕重命名操作添加Catch
语句。如果发生错误,将执行For i = 0 To Math.Min(oldfldrnme.Count, newfldrnme.Count) - 1
Dim DestPath As String = Path.Combine(dest, oldfldrnme(i)) 'This is the file/directory to be renamed.
If File.Exists(DestPath) Then
Try
Microsoft.VisualBasic.FileIO.FileSystem.RenameFile(DestPath, newfldrnme(i))
Catch ex As Exception
'Log error using ex.Message. For example "Could not rename file: " & ex.Message
End Try
ElseIf Directory.Exists(DestPath) Then
Try
Microsoft.VisualBasic.FileIO.FileSystem.RenameDirectory(DestPath, newfldrnme(i))
Catch ex As Exception
'Log error using ex.Message. For example "Could not rename folder: " & ex.Message
End Try
End If
Next
块,这意味着您没有停止整个应用程序的问题。
productDatePickerDialog = new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener()
{
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
{
Calendar newDate = Calendar.getInstance();
newDate.set(year, monthOfYear, dayOfMonth);
Log.v("Date", "date " + dateFormatter.format(newDate.getTime()));
updateDate(dateFormatter.format(newDate.getTime()));
}}, newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH),
newCalendar.get(Calendar.DAY_OF_MONTH));
holder.edt_Three.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
productDatePickerDialog.show();
}
});
private void updateDate(String dDate) {
Log.v("Date", "date " + dDate);
//TODO
holder.edt_Three.setText(dDate);
notifyDataSetChanged();
}
答案 1 :(得分:1)
如果对不存在的项进行调用,GetAttributes
函数将抛出异常。怎么可能不这样呢?不存在的东西没有属性。因此,它不是检查某些东西是否存在的一般解决方案。
您需要使用Exists
方法。例如:
Public Enum FileSystemObject
InvalidPath
Directory
File
End Enum
Public Function WhatIsThisThing(String path) As FileSystemObject
If Directory.Exists(path) Then
Return FileSystemObject.Directory
Else If File.Exists(path)
Return FileSystemObject.File
Else
Return FileSystemObject.InvalidPath
End If
End Function
但是,您仍然可以在此处执行竞争条件,就像执行文件I / O时一样。事先执行检查很好,但是 还需要处理在尝试重命名对象时可能引发的异常。您无法确保在检查文件或文件夹的时间与发出重命名命令的时间之间没有删除或重命名文件或文件夹。
答案 2 :(得分:0)
用于检查VB.NET中存在的路径/文件,
If File.Exists(strPath) Then
' yes this is a file
Else
' oh no
End If
答案 3 :(得分:0)
你可以试试这个:
Imports System.IO
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ListBox1.Items.Clear()
Listfolders(TextBox1.Text) 'Call Public Sub ListFolders
End Sub
Public Sub Listfolders(ByVal Path As String) 'Path is Texbox1.Text
Dim DirInfo As New IO.DirectoryInfo(Path) 'C:\!Testfolder
Dim FileObject As IO.FileSystemInfo
For Each FileObject In DirInfo.GetFileSystemInfos 'fileobject is file or folder
If FileObject.Attributes = IO.FileAttributes.Directory Then
'Fileobject is Folder
Listfolders(FileObject.FullName) 'Use public Sub again to find folders and files in subfolders
ListBox1.Items.Add(FileObject.Name) 'Add folder to listbox
Else
'Fileobject is file
ListBox1.Items.Add(FileObject.Name) 'Add file to listbox
End If
Next
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TextBox1.Text = "C:\!TestFolder" 'Give initial text to Textbox1
End Sub
End Class