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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8352708/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 15:47:20  来源:igfitidea点击:

How to check if file already exists in the folder

vb.net

提问by Furqan Sehgal

I am trying to copy some files to folder. I am using the following statement to check if the source fie exists

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

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

But I donot know how to check if file exists in the folder before copying. Please advise.

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

Thanks and best regards, Furqan

谢谢和最好的问候, Furqan

回答by Bradley Uffner

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

回答by Muhammed Farooq

'In Visual Basic

'在 Visual Basic 中

Dim FileName = "newfile.xml" ' The Name of file with its Extension Example A.txt or A.xml

Dim FilePath ="C:\MyFolderName" & "\" & FileName  'First Name of Directory and Then Name of Folder if it exists and then attach the name of file you want to search.

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