如何检查文件夹中是否已存在文件
问题描述:
我试图将一些文件复制到文件夹。我使用下面的语句来检查源外商投资企业存在如何检查文件夹中是否已存在文件
If My.Computer.FileSystem.FileExists(fileToCopy) Then
但我DONOT知道如何检查文件的文件夹中是否存在复制之前。 请指教。
感谢和问候, Furqan
答
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
+0
我想我终于明白了你想要了解的内容,让我知道更新的代码是否更接近你的意图。 –
如果我理解正确的话,你实际上是问如何从原来的路径拉断的文件名,看是否有相同名称的文件不同的目录中存在?这是现在唯一对我有意义的解释,因为你显然已经知道如何查看文件是否存在。正如我的示例所示,您可以使用'system.io.path'类来处理路径。请让我们知道这实际上是你的意思。 –