Use a TextField to describe a path to save the files

1

Good morning, I'm trying to create a system that when I type a name in a textfield it would create a folder with this name and add the file inside the folder.

The part of adding the file is correct, I need to know how to create it.

I'm very lay in programming yet, I'll send you a line as an example:

File.WriteAllText(System.IO.Path.Combine(projectPath,@"novapasta.Domain\Entities\" + varPath + "\" + entityName + ".cs"), Template);

the path would be in txtPath

    
asked by anonymous 20.07.2016 / 19:13

2 answers

2

Use the Directory.CreateDirectory method. It will create all necessary directories and subdirectories, unless they exist, as per the documentation.

  

Creates all directories and subdirectories in the specified path   unless they already exist.

The method below will create the temp and files folders on the C disk.

Directory.CreateDirectory(@"C:\temp\files");

To create a file in a specific directory, regardless of whether it exists or not, you can use something like this:

public void EscreverNoArquivo(string path, string filename, string content)
{
   Directory.CreateDirectory(path);
   System.IO.File.WriteAllText($"{path}\{filename}", content);
}
    
20.07.2016 / 20:00
-1
public static bool CreateFolder(string folder)
{
    if (Directory.Exists(folder)) return true;
    else
    {
        try
        {
            Directory.CreateDirectory(folder);
            return true;
        }
        catch (Exception e)
        {
            return false;
        }
    }
}
    
20.07.2016 / 19:51