Check if a series of directories of a path exists in VB.NET

0

I am still junior programmer and would like to know if in VB.NET it is possible to create a series of directory checks to see if it exists (not to confuse with handle the direct url and verify); I wanted it more or less like this:

I have a C:\Victor\Programas\Teste path, I wanted my program to check if each directory of that path exists, and if it does not exist, create the directory, and then copy the junior.txt file into this directory.

Editing

I did not really know that Create Directory already did the part of creating the directory in case it does not exist. But I have one last question if it is not to be bothered. My program has the function of copying this file to a certain directory, inside the program directory, as I do not need to check each one I would have to check the parent directory of the right file? to use the create directory method if it does not exist ..   But the question is how do I get this last parent directory?

I have more or less this:

Dim DiretorioOriginal as String = "C:Usures\Victor\Desktop\TesteDiretorios\$Teste\Lu\Ui.txt"

The "$" is for min to use the fear split being only Test \ Lu \ Ui.txt < this directory will be the same as in the program

I'm going to copy this file to the program directory:

Dim DiretorioProgran as string = application.startup & "\TesteDiretorios\"

In case I'm going to create the folder ** test \ lu **

Would it be something like this?

Dim r as directoryinfo 
r = new directoryinfo(no caso a string Teste\Lu\Ui.txt)
dim result as string = r 

if system.io.directory.exists("C:Usures\Victor\Desktop\TesteDiretorios\$Teste\Lu\Ui.txt")= true then 


else

system.io.Directory.createdirecory("C:Usures\Victor\Desktop\TesteDiretorios\$Teste\Lu\")

end if 
    
asked by anonymous 11.11.2014 / 18:37

1 answer

1

You do not need to check each subdirectory ... the Directory.CreateDirectory method will create all levels of the directory automatically passed to you.

Furthermore, there is no safe way to check with Directory.Exists and then call Directory.CreateDirectory . If another thread or another program creates / deletes the directory in the meantime between checking for Exists and creation with CreateDirectory , you could end up with an unhandled exception.

The safest way is as follows:

  • The C: drive is the only part of the directory that actually exists in the example below:

    Try
        Directory.CreateDirectory("C:\masb\xpto\hifdskgfi\giufdagifgdw")
    Catch
        MessageBox.Show("Não foi possível criar o diretório")
    End Try
    

If you enter the catch block, then it is because:

  • A file that already exists matches the name of one of the subdirectories
  • the user does not have permission

To know which one, you'll need to check what the exception is.

Editing

The code copying the file should not make checks using Exists . It's almost never a good idea to use the Exists method. It would look like this:

Dim DiretorioArquivoOriginal As String = "C:\Users\Victor\Desktop\TesteDiretorios\$Teste\Lu\Ui.txt"
Dim Subdiretorio As String() = DiretorioArquivoOriginal.Split("$".ToCharArray(), StringSplitOptions.None)
If Subdiretorio.Length = 2 Then
    Dim DiretorioArquivoPrograma As String = Path.Combine(Application.StartupPath, "TesteDiretorios", Subdiretorio(1))
    Dim DiretorioPrograma As String = Path.GetDirectoryName(DiretorioArquivoPrograma)
    Try
        Directory.CreateDirectory(DiretorioPrograma)
        File.Copy(DiretorioArquivoOriginal, DiretorioArquivoPrograma)
    Catch
        MessageBox.Show("Não foi possível copiar o arquivo.")
    End Try
End If

Notes:

  • I used Path.Combine to concatenate directory names.

  • Path.GetDirectoryName is used to get the name of the parent directory of the file.

11.11.2014 / 22:13