1
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);
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".
– Leandro Angelo