How to Create A Simple Program For Hiding Folders with C#

Want to hide your folders with a program you've created? Or have you ever thought of creating a software program that hides folders? If you have, then you are in the right place to learn how to hide folders in C#.

C# makes it easier for you to create a program that simply hides and unhides folders.

We have created a directory info “ch” object which is used to get the properties of a folder and then get the folder directory. After that, we instantiated the directoryinfo object and assigned it to the folder directory. Now, we just change the file attributes of the directory to either Hidden to hide it or Normal to unhide it.

Open Visual Studio, click on New Project -> Windows Form Application and give it any name you want. You can enter "HideFolder" as the name.

Next, add buttons, labels, textbox and folderBrowserDialog to your blank form and design it.

Then, rename the controls as listed below. You can change the properties of controls by clicking on the controls and change the text and name from the Properties dialog box located at the bottom right corner.

Text Name
Browse - btnBrowse
Hide - btnHide
UnHide - btnUnhide
Open- btnOpen
textbox1- txtFilePath

Now, double click on "Browse" button and copy and paste the below code.
  1. if (folderBrowserDialog1.ShowDialog() == DialogResult.OK){  
  2. txtFilePath.Text = folderBrowserDialog1.SelectedPath;  
  3. }  
Double click on "Hide" button and paste the below code.
  1. try{  
  2. ch = new DirectoryInfo(txtFilePath.Text);  
  3. ch.Attributes = FileAttributes.Hidden;  
  4. MessageBox.Show("Hidden");  
  5. }  
  6. catch { }  
Double click on "Open" button and paste the below code.
  1. try{ System.Diagnostics.Process.Start(txtFilePath.Text); } catch { }   
Double click on "Unhide" button and paste the below code.
  1. try{  
  2.  ch = new DirectoryInfo(txtFilePath.Text);  
  3.  ch.Attributes = FileAttributes.Normal;   
  4. MessageBox.Show("Folder Visible");  
  5.  } catch (Exception ex){MessageBox.Show("Error Occured due to" + ex); }  

Then, update your Form1.cs with the code given below.

Form1.cs 
  1. using System;  
  2.   
  3. using System.IO;  
  4.   
  5. using System.Windows.Forms;  
  6.   
  7. namespace HideFolder  
  8.   
  9. {  
  10.   
  11. public partial class Form1 : Form  
  12.   
  13. {  
  14.   
  15. DirectoryInfo ch;  
  16.   
  17. public Form1()  
  18. {  
  19.   
  20. InitializeComponent();  
  21.   
  22. }  
  23.   
  24. private void btnBrowse_Click(object sender, EventArgs e){  
  25.   
  26. if (folderBrowserDialog1.ShowDialog() == DialogResult.OK){  
  27.   
  28. txtFilePath.Text = folderBrowserDialog1.SelectedPath;  
  29.   
  30. }  
  31.   
  32. }  
  33.   
  34. private void btnOpen_Click(object sender, EventArgs e){  
  35.   
  36. try{  
  37.   
  38. System.Diagnostics.Process.Start(txtFilePath.Text);  
  39.   
  40. }  
  41.   
  42. catch { }  
  43.   
  44. }  
  45.   
  46. private void btnHide_Click(object sender, EventArgs e){  
  47.   
  48. try{  
  49.   
  50. ch = new DirectoryInfo(txtFilePath.Text);  
  51.   
  52. ch.Attributes = FileAttributes.Hidden;  
  53.   
  54. MessageBox.Show("Hidden");  
  55.   
  56. }  
  57. catch { }  
  58.   
  59. }  
  60.   
  61. private void btnUnhide_Click(object sender, EventArgs e){  
  62.   
  63. try{  
  64.   
  65. ch = new DirectoryInfo(txtFilePath.Text);  
  66.   
  67. ch.Attributes = FileAttributes.Normal;  
  68.   
  69. MessageBox.Show("Visible");  
  70.   
  71. }  
  72. catch { }  
  73. }  
  74.   
  75. }  
  76.   
  77. }  
  • Now, run your project using F5.
  • Then, click on "Browse a folder" to select the folder you want to hide. 
  • The folder path will be shown in the textbox.
  • Click on "Hide" button, and the folder will be hidden and you will get a message.
That's it. Thanks for reading.