如何检查文件夹中是否已存在文件

时间:2011-12-02 06:48:16

标签: vb.net

我正在尝试将一些文件复制到文件夹中。我使用以下语句来检查源是否存在

 If My.Computer.FileSystem.FileExists(fileToCopy) Then

但我不知道在复制之前如何检查文件夹中是否存在文件。 请指教。

谢谢和最诚挚的问候, Furqan

2 个答案:

答案 0 :(得分:46)

Dim SourcePath As String = "c:\SomeFolder\SomeFileYouWantToCopy.txt" 'This is just an example string and could be anything, it maps to fileToCopy in your code.
Dim SaveDirectory As string = "c:\DestinationFolder"

Dim Filename As String = System.IO.Path.GetFileName(SourcePath) 'get the filename of the original file without the directory on it
Dim SavePath As String = System.IO.Path.Combine(SaveDirectory, Filename) 'combines the saveDirectory and the filename to get a fully qualified path.

If System.IO.File.Exists(SavePath) Then
   'The file exists
Else
    'the file doesn't exist
End If

答案 1 :(得分:-2)

'在Visual Basic中

name