2011-12-02 26 views
9

Sto provando a copiare alcuni file nella cartella. Sto usando la seguente dichiarazione per verificare se il fie di origine esisteCome verificare se il file esiste già nella cartella

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

Ma io donot sapere come verificare se il file esiste nella cartella prima di copiare. Si prega di avvisare.

Grazie e cordiali saluti, Furqan

+0

Se ho capito bene si sta effettivamente chiedendo come staccare il nome del file dal percorso originale e vedere se un file con lo stesso nome esiste all'interno di una directory diversa? Questa è l'unica interpretazione che ha senso per me ora, dal momento che ovviamente sai già come vedere se esiste un file. Puoi usare la classe 'system.io.path' per manipolare i percorsi come mostra il mio esempio. Fateci sapere se questo è effettivamente ciò che intendete. –

risposta

35
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

Penso di aver finalmente capito cosa stavi cercando di ottenere, fammi sapere se il codice aggiornato è più vicino a quello che intendi. –

Problemi correlati