Use a Textfield to describe a path to save the files

Asked

Viewed 71 times

1

Good morning guys, 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 do to create the folder.

I am very layman in programming yet, I will send 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

  • 2

    What language? What kind of application? What question are you having?

  • It’s C# and my question is how do I get the system to "create" this folder, which I would type in the textfield Path (which I’ve already turned into a variable). But I have no idea how I would do that.

  • It would be interesting then you edit the question, add the language tag and this additional information, the question is confused as it was posted;.

  • ok, first time I used it here. Thanks

2 answers

2

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

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

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

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

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

public void EscreverNoArquivo(string path, string filename, string content)
{
   Directory.CreateDirectory(path);
   System.IO.File.WriteAllText($"{path}\\{filename}", content);
}

-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;
        }
    }
}
  • I did something similar but it was successful only if the folder already existed, if the folder did not exist it does not create and says that the folder does not exist. See what I did wrong please ; String subPath = txtPath.Text;
 if (!Directory.Exists(subPath))
 {
 
 Directory.CreateDirectory(subPath);
 }

  • the code seems correct, you would have to check if the subPath string is correct. Or if it is the path to a file. System.IO.Path.HasExtension(String path) returns true if it has extension, or false if not, it should help when checking whether the path is actually a folder or file.

  • If I used your code, as I add the text of my variable to the name of the Folder?

  • I don’t think I understand your question. But if it is what I understand then just call the method CreateFolder(suavariavel);. A possibility for the error you described in the first question, is if you have create a folder right in the root directory. Something like C:\teste would be prevented by Windows. To be possible you would have to assign administrator right to the program.

  • 1

    First you are making extreme misuse of the try-catch. You do not need to check if the directory exists before creating it, if it already exists, nothing will happen. Consequently, all this function is unnecessary and prolific.

  • @jbueno It actually depends on the context. The code for this answer is the most performative and thread-safe that could be created using just the .Net. Use only the if would make the set not thread-safe, use only the try/catch would bring performance loss every time the directory actually exists: a loop around the call of this method with thousands of exceptions being released would be pretty bad if performance is a consideration.

Show 1 more comment

Browser other questions tagged

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