Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to create file and folder inside another folder in C#.
Note: file.txt
Posted

Having read the solutions posted and your comments back, there is too much "I want" and not enough "This is what I have done and this is where I have got stuck" i.e. you are not making any effort yourself.

I suggest you go and read the documentation available on what the various classes in the System.io namespace do, and how you can use them to achieve your task.

System.IO Namespace[^]
 
Share this answer
 
 
Share this answer
 
Comments
engmebeed 23-Oct-12 6:39am    
Not the solution if you could write the simple code
Ambesha 23-Oct-12 6:48am    
should open the links at once
[no name] 23-Oct-12 6:53am    
it will gr8 if you can look into the url provided by nelek. i have copy pasted the contents from there only.
engmebeed 23-Oct-12 7:07am    
This code create folder inside folder and file inside the last folder , i want to create file and folder in same folder like tree with two branches
Freind, This is the dish served to your plate:

C#
public class CreateFileOrFolder
{
    static void Main()
    {
        // Specify a "currently active folder"
        string activeDir = @"c:\testdir2";

        //Create a new subfolder under the current active folder
        string newPath = System.IO.Path.Combine(activeDir, "mySubDir");

        // Create the subfolder
        System.IO.Directory.CreateDirectory(newPath);

        // Create a new file name. This example generates
        // a random string.
        string newFileName = System.IO.Path.GetRandomFileName();

        // Combine the new file name with the path
        newPath = System.IO.Path.Combine(newPath, newFileName);

        // Create the file and write to it.
        // DANGER: System.IO.File.Create will overwrite the file
        // if it already exists. This can occur even with
        // random file names.
        if (!System.IO.File.Exists(newPath))
        {
            using (System.IO.FileStream fs = System.IO.File.Create(newPath))
            {
                for (byte i = 0; i < 100; i++)
                {
                    fs.WriteByte(i);
                }
            }
        }

        // Read data back from the file to prove
        // that the previous code worked.
        try
        {
            byte[] readBuffer = System.IO.File.ReadAllBytes(newPath);
            foreach (byte b in readBuffer)
            {
                Console.WriteLine(b);
            }
        }
        catch (System.IO.IOException e)
        {
            Console.WriteLine(e.Message);
        }

        // Keep the console window open in debug mode.
        System.Console.WriteLine("Press any key to exit.");
        System.Console.ReadKey();
    }
}


Thanks,
Ambesha
 
Share this answer
 
Comments
[no name] 23-Oct-12 6:56am    
Hahahahaha... gr8 starting line.. :)
engmebeed 23-Oct-12 7:06am    
This code create folder inside folder and file inside the last folder , i want to create file and folder in same folder like tree with two branches
Ambesha 23-Oct-12 7:14am    
specify path and code that you have try to done it
This is the code

C#
// Specify a "currently active folder"
       string activeDir = @"c:\testdir2";
       //Create a new subfolder under the current active folder
       string newPath = System.IO.Path.Combine(activeDir, "mySubDir");
       // Create the subfolder
       System.IO.Directory.CreateDirectory(newPath);
       // Create a new file name. This example generates
       // a random string.
       string newFileName = System.IO.Path.GetRandomFileName();
       // Combine the new file name with the path
       newPath = newPath+"\\mySubDir" + newFileName;
       // create a new file.
       System.IO.File.Create(newPath);
 
Share this answer
 
v2
Comments
engmebeed 23-Oct-12 7:06am    
This code create folder inside folder and file inside the last folder , i want to create file and folder in same folder like tree with two branches
[no name] 23-Oct-12 7:11am    
it is just a matter of changing the path. i have did that in my code. pls check.
right click on folder in which you want to create -- then add -- folder
 
Share this answer
 
Comments
Ambesha 23-Oct-12 7:51am    
What does it means Satbir ??
engmebeed 23-Oct-12 8:03am    
What??

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900