List existing folders/subfolders and files in a specific root folder

Asked

Viewed 953 times

1

It is possible via code to list existing folders/files in a specific folder ?

Example inserir a descrição da imagem aqui

And play this list on a grid/table on the WEB for the user to know if the folder he wants to create already exists?

  • Yes, it’s possible, but not the most recommended. You can simply try to create the folder, capture the error if it already exists and pass this return to the user. Another common practice is to use guids to create folders and save your nickname in the bank. So all users could have their folder called "Test".

1 answer

3


Yes, it is possible, but it is not the most recommended. You can simply test if it already exists and pass this return to the user.

Another common practice is to use guids to create folders and save your nickname in the bank. So all users could have their folder called "Test".

To list you can from that code:

string caminho = @"C:\Raiz\";
foreach (string item in Directory.GetDirectories(caminho))
{
    Console.WriteLine(item.Remove(0, caminho.Length));
}

To check if the directory does not exist you should consult before, because the method Directory.CreateDirectory() will only fail if the path is invalid or there is some security restriction. If it already exists it just ignores the instruction.

string raiz = @"C:\Raiz\";
string novoDir = "IR";
string mensagem = string.Empty;


if (!Directory.Exists(raiz + novoDir))
{
    Directory.CreateDirectory(raiz + novoDir);
    mensagem = string.Format("O diretório \"{0}\" foi criado com sucesso!", novoDir);
}
else
{
    mensagem = string.Format("O diretório \"{0}\" já existe, escolha um novo nome!", novoDir);
}

Console.WriteLine(mensagem);
  • In fact the idea would be not store in the bank but already store the uploads/creation of new folders right in the physical directory!

  • And when it comes to consulting, how will you know what belongs to each user?

  • There will be no such validation, the root folder will be a place to only make tutorials available to all users

  • 1

    Okay, is there any more doubt?

Browser other questions tagged

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