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

Asked

Viewed 1,682 times

0

I’m still a junior programmer and I wonder if in VB.NET it is possible to create a series of directory checks to know if it exists (do not confuse with take the direct url and check); wanted more or less like this:

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

Editing

I really didn’t know that Create Directory was already part of creating the directory in case it did not.. But I have one last question if you don’t mind. 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 in case it does not exist.. But the question is this how do I get this last parent directory?

I have but that:

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

The "$" is for me to use the split fear getting alone Test Lu Ui.txt << this directory will be the same as inside the program

I will copy this file to the program directory:

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

in the case how I will create the **test lu folder**

would be but so or so?

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 
  • If you really want to know if there is each of them separately, divide the string by the bar, and make a loop that goes testing piece by piece. C:, C: Victor, C: Victor Programs etc.

  • Fought to answer, but I tried to make the loop and did not succeed, if you can show me but or less how I can do it, I thank.

  • I edited my reply with additional information you requested. I took the liberty of incorporating to your original question, the text posted as answer (you can edit your questions when you want to increase them).

1 answer

1


No need to check each subdirectory... the method Directory.CreateDirectory will create all levels of the directory passed automatically for you.

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

The safest way is as follows:

  • The drive C: 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, it will be necessary to verify which is the exception.

Editing

Code copying file should not do checks using Exists. It’s almost never a good idea to use the method Exists. 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 the Path.Combine to concatenate directory names.

  • Path.GetDirectoryName is used to take the name of the file’s parent directory.

  • Thank you for answering me if you can help me with this final question I thank you.

  • What do you mean? The moving part of the file? Or the checking which exception occurred, when any?

  • I just want to copy the file to the same directory of the program because it has a copy of the folder but there is a folder that n exists so I wanted to know how would use such method before the copy to create this directory that n exists.

  • Thank you Miguel, I had a lot of trouble with the directory exist and this way it really works normal. Thanks again.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.