Search This Blog

  • ()
  • ()
Show more

How to: Create a File or Folder (C# Programming Guide)

You can programmatically create a folder on your computer, create a subfolder, create a file in the subfolder, and write data to the file. E...








You can programmatically create a folder on your computer, create a subfolder, create a file in the subfolder, and write data to the file.



Example








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 FolderSubFolder2";

// 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 FolderSubFolderttxvauxe.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
}








If the folder already exists, CreateDirectorydoes nothing, and no exception is thrown. However, File.Create replaces an existing file with a new file. The example uses an if-elsestatement to prevent an existing file from being replaced.

By making the following changes in the example, you can specify different outcomes based on whether a file with a certain name already exists. If such a file doesn't exist, the code creates one. If such a file exists, the code appends data to that file.

  • Specify a non-random file name.






    // Comment out the following line.
    //string fileName = System.IO.Path.GetRandomFileName();

    // Replace that line with the following assignment.
    string fileName = "MyNewFile.txt";






  • Replace the if-else statement with theusing statement in the following code.






    using (System.IO.FileStream fs = new System.IO.FileStream(pathString, FileMode.Append)) 
    {
    for (byte i = 0; i < 100; i++)
    {
    fs.WriteByte(i);
    }
    }







Run the example several times to verify that data is added to the file each time.

For more FileMode values that you can try, see FileMode.

The following conditions may cause an exception:





.NET Framework Security




An instance of the SecurityException class may be thrown in partial-trust situations.

If you don’t have permission to create the folder, the example throws an instance of theUnauthorizedAccessException class.





COMMENTS

Name

apple,2,Article,14,At home,13,Author,14,Beauty,1,Biography,2,blackberry,1,Business,6,Cars,5,Celebrity,13,conspiraci,15,Fashion,5,galaxy,1,Gallery,1,Games,11,google,1,Hair,1,Health amp; Fitness,2,Histori,11,Home,169,HOSTING,27,HTML,7,imac,1,Image,1,iphone,1,Itcyber,7,Kuran,31,Lajme,34,Life amp; Love,1,Makeup amp; Skincare,1,mobile,5,monitor,1,Movies,2,News,5,No category,5,Photography,6,PHP,30,Poezi,24,POEZI amp; TEKSTE,2,Post,14,PROGRAMMIM,46,Relationships,1,review,6,samsung,1,Seo,7,Softvare,4,Sport,5,Sports,7,Stars,5,Tag,7,Tags,7,Tech,30,Teknologji,37,THEMES,3,Tutorials,191,Video,47,Videos,4,Vip News,2,Webhosting,67,WordPress,7,World,13,
ltr
item
Cr337: How to: Create a File or Folder (C# Programming Guide)
How to: Create a File or Folder (C# Programming Guide)
Cr337
https://cr337.blogspot.com/2016/03/how-to-create-file-or-folder-c.html
https://cr337.blogspot.com/
http://cr337.blogspot.com/
http://cr337.blogspot.com/2016/03/how-to-create-file-or-folder-c.html
true
5516280490839897951
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy