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");
public class CreateFileOrFolder
{
static void Main()
{
// Specify a name for your top-level folder.
string folderName = @"c:\Top-Level Folder";
// To create a string that specifies the path to a subfolder under your
// top-level folder, add a name for the subfolder to folderName.
string pathString = System.IO.Path.Combine(folderName, "SubFolder");
// You can write out the path name directly instead of using the Combine
// method. Combine just makes the process easier.
string pathString2 = @"c:\Top-Level Folder\SubFolder2";
// You can extend the depth of your path if you want to.
//pathString = System.IO.Path.Combine(pathString, "SubSubFolder");
// Create the subfolder. You can verify in File Explorer that you have this
// structure in the C: drive.
// Local Disk (C:)
// Top-Level Folder
// SubFolder
System.IO.Directory.CreateDirectory(pathString);
// Create a file name for the file you want to create.
string fileName = System.IO.Path.GetRandomFileName();
// This example uses a random string for the name, but you also can specify
// a particular name.
//string fileName = "MyNewFile.txt";
// Use Combine again to add the file name to the path.
pathString = System.IO.Path.Combine(pathString, fileName);
// Verify the path that you have constructed.
Console.WriteLine("Path to my file: {0}\n", pathString);
// Check that the file doesn't already exist. If it doesn't exist, create
// the file and write integers 0 - 99 to it.
// DANGER: System.IO.File.Create will overwrite the file if it already exists.
// This could happen even with random file names, although it is unlikely.
if (!System.IO.File.Exists(pathString))
{
using (System.IO.FileStream fs = System.IO.File.Create(pathString))
{
for (byte i = 0; i < 100; i++)
{
fs.WriteByte(i);
}
}
}
else
{
Console.WriteLine("File \"{0}\" already exists.", fileName);
return;
}
// Read and display the data from your file.
try
{
byte[] readBuffer = System.IO.File.ReadAllBytes(pathString);
foreach (byte b in readBuffer)
{
Console.Write(b + " ");
}
Console.WriteLine();
}
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();
}
// Sample output:
// Path to my file: c:\Top-Level Folder\SubFolder\ttxvauxe.vv0
//0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
//30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
// 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 8
//3 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
}
Saeid_12597
None
0 Points
90 Posts
Create Folder
Dec 16, 2012 03:58 AM|LINK
Hi I Want to Create Folder By Programing In Path On Hard?
I Thank For Help Me
oned_gk
All-Star
31832 Points
6516 Posts
Re: Create Folder
Dec 16, 2012 04:07 AM|LINK
sarathi125
Star
13599 Points
2691 Posts
Re: Create Folder
Dec 17, 2012 03:38 AM|LINK
Hi,
Use the below code as per http://forums.asp.net/p/1226236/2209871.aspx:
string subPath ="ImagesPath"; // your code goes here
bool IsExists = System.IO.Directory.Exists(Server.MapPath(subPath));
if(!IsExists)
System.IO.Directory.CreateDirectory(Server.MapPath(subPath));
Remember to click Mark as Answer on the post that helps to others.
My Blog :MyAspSnippets
RameshRajend...
Star
7983 Points
2099 Posts
Re: Create Folder
Dec 17, 2012 03:55 AM|LINK
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");http://stackoverflow.com/questions/3695163/c-sharp-filestream-and-creating-folders
http://www.dotnetperls.com/directory-createdirectory
sachintek
Member
570 Points
141 Posts
Re: Create Folder
Dec 17, 2012 04:26 AM|LINK
public class CreateFileOrFolder { static void Main() { // Specify a name for your top-level folder. string folderName = @"c:\Top-Level Folder"; // To create a string that specifies the path to a subfolder under your // top-level folder, add a name for the subfolder to folderName. string pathString = System.IO.Path.Combine(folderName, "SubFolder"); // You can write out the path name directly instead of using the Combine // method. Combine just makes the process easier. string pathString2 = @"c:\Top-Level Folder\SubFolder2"; // You can extend the depth of your path if you want to. //pathString = System.IO.Path.Combine(pathString, "SubSubFolder"); // Create the subfolder. You can verify in File Explorer that you have this // structure in the C: drive. // Local Disk (C:) // Top-Level Folder // SubFolder System.IO.Directory.CreateDirectory(pathString); // Create a file name for the file you want to create. string fileName = System.IO.Path.GetRandomFileName(); // This example uses a random string for the name, but you also can specify // a particular name. //string fileName = "MyNewFile.txt"; // Use Combine again to add the file name to the path. pathString = System.IO.Path.Combine(pathString, fileName); // Verify the path that you have constructed. Console.WriteLine("Path to my file: {0}\n", pathString); // Check that the file doesn't already exist. If it doesn't exist, create // the file and write integers 0 - 99 to it. // DANGER: System.IO.File.Create will overwrite the file if it already exists. // This could happen even with random file names, although it is unlikely. if (!System.IO.File.Exists(pathString)) { using (System.IO.FileStream fs = System.IO.File.Create(pathString)) { for (byte i = 0; i < 100; i++) { fs.WriteByte(i); } } } else { Console.WriteLine("File \"{0}\" already exists.", fileName); return; } // Read and display the data from your file. try { byte[] readBuffer = System.IO.File.ReadAllBytes(pathString); foreach (byte b in readBuffer) { Console.Write(b + " "); } Console.WriteLine(); } 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(); } // Sample output: // Path to my file: c:\Top-Level Folder\SubFolder\ttxvauxe.vv0 //0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 //30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 // 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 8 //3 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 }