Create Folder using C#

This is a continuation of my other tutorial entitled Delete Folder and Its Content using C# but now we will create here a program that will create a folder using C# also. Now, let's start this tutorial! 1. Let's start with creating a Windows Form Application in C# for this tutorial by following the following steps in Microsoft Visual Studio: Go to File, click New Project, and choose Windows Application and name your project as Create Folder. 2. Next, add only one Button named Button1 and labeled it as "Create Folder". Add also TextBox named TextBox1 for our inputting the path of the folder that you want to delete. You must design your interface like this: design 3. To achieve that, first of all we need to include the following statement in the program code: using System.IO - This namespace has to precede the whole program code as it is higher in hierarchy for DirectoryInfo. In Fact, this is the concept of object oriented programming where DirectoryInfo is part of the namespace System.IO.
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8.  
  9. using System.IO;
4. Now to create a folder, we will now code for Button1.
  1. public void Button1_Click(System.Object sender, System.EventArgs e)
  2. {
  3. DirectoryInfo di = new DirectoryInfo(TextBox1.Text);
  4. if (di.Exists)
  5. {
  6. MessageBox.Show("That path exists already.");
  7. TextBox1.Clear();
  8. }
  9. else
  10. {
  11. di.Create();
  12. Interaction.MsgBox("Folder Is Created.......!", MsgBoxStyle.Information, "Create Folder");
  13. TextBox1.Clear();
  14. }
  15. }
The DirectoryInfo class above is used to locate and have a directory of a file or folder. It passes the value of the directory as the input of Textbox1. If directory is already existed (di.Exists) then it will create the TextBox1 and display "That path exists already.". Otherwise, it will create a New Folder saved in the Directory and Folder name that corresponds to TextBox1. It will also clear the value of the TextBox1 and display "Folder Is Created.......!". Now, try to type this on your TextBox1: C:/sample. Then Click Button1. This will display the result that the folder is created. Then if you type again C:/sample, the program will prompt the user that the path is already existed. output Full source code:
  1. using System.Diagnostics;
  2. using System;
  3. using System.Windows.Forms;
  4. using System.Collections;
  5. using System.Drawing;
  6. using System.Data;
  7. using System.Collections.Generic;
  8.  
  9.  
  10. using System.IO;
  11.  
  12. namespace Create_and_Delete_a_Folder
  13. {
  14. public partial class Form1
  15. {
  16. public Form1()
  17. {
  18. InitializeComponent();
  19.  
  20.  
  21. }
  22.  
  23. public void Button1_Click(System.Object sender, System.EventArgs e)
  24. {
  25. DirectoryInfo di = new DirectoryInfo(TextBox1.Text);
  26. if (di.Exists)
  27. {
  28. MessageBox.Show("That path exists already.");
  29. TextBox1.Clear();
  30. }
  31. else
  32. {
  33. di.Create();
  34. Interaction.MsgBox("Folder Is Created.......!", MsgBoxStyle.Information, "Create Folder");
  35. TextBox1.Clear();
  36. }
  37. }
  38.  
  39.  
  40. }
  41.  
  42. }
Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer If you have some queries, feel free to contact the number or e-mail below. Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment