Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

creating a new folder and a text file inside that folder

Tags:

c#

I wanna create a new folder named log and inside that folder i want to create a textfile named log.txt and this is the path i want to create D:\New Folder

i have tried this to create a new folder named log

string FilePathSave = Folder.ToString() + System.IO.Directory.CreateDirectory(@"D:\New Folder\Log");

And i have created a text file using this

string FilePathSave = Folder.ToString() +"log.txt";
File.WriteAllText(FilePathSave, TextBox1.Text);                

But i cant create like D:\New Folder\Log\log.txt...any suggestion??

like image 966
bala3569 Avatar asked Nov 22 '10 10:11

bala3569


People also ask

Can we create a folder within a folder?

Tip: You can also right-click any folder in the Folder Pane and click New Folder. Type your folder name in the Name text box. In the Folder Contains drop-down menu, click Mail and Post Items. In the Select where to place the folder box, click the folder under which you want to place your new subfolder.

What are the steps to create a new folder?

Procedure. Click Actions, Create, Folder. In the Folder name box, type a name for the new folder. Click Next.

Can you put a folder inside a file?

Windows uses folders to help you organize files. You can put files inside a folder, just like you would put documents inside a real folder.


2 Answers

Urm, something like this?

var dir = @"D:\New folder\log";  // folder location

if(!Directory.Exists(dir))  // if it doesn't exist, create
    Directory.CreateDirectory(dir);

// use Path.Combine to combine 2 strings to a path
File.WriteAllText(Path.Combine(dir, "log.txt"), "blah blah, text");
like image 58
Jan Jongboom Avatar answered Sep 20 '22 19:09

Jan Jongboom


    string dir = @"D:\New Folder\Log";
    if (!Directory.Exists(dir))
    {
        Directory.CreateDirectory(dir);
    }

    File.WriteAllText(Path.Combine(dir, "log.txt"), TextBox1.Text);
like image 43
Stefan P. Avatar answered Sep 20 '22 19:09

Stefan P.