Hi,
Hope you have read my previous post on Lock a Folder in C#. Now here you can learn How to Hide a Folder in C#. So let's start;
Steps
1. Open Visual Studio -> New Project -> Windows Form Application with a name
HideFolder.
2. Now add buttons, label, textbox and folderBrowserDialog to your blank form
and design it as shown in the below screenshot.
3. Rename the controls as given below; (Click on the buttons & textbox and change
the Text and Name from the Properties dialog box)
Text Name
Browse - btnBrowse
Hide - btnHide
UnHide - btnUnhide
Open- btnOpen
textbox1- txtFilePath
4. Now double click on Browse button and copy the below code;
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
txtFilePath.Text = folderBrowserDialog1.SelectedPath;
}
5. Now double click on Hide button and copy the below code;
try
{
ch = new DirectoryInfo(txtFilePath.Text);
ch.Attributes = FileAttributes.Hidden;
MessageBox.Show("Hidden");
}
catch { }
6. Now double click on Open button and copy the below code;
try
{
System.Diagnostics.Process.Start(txtFilePath.Text);
}
catch { }
7. Also double click on Unhide button and copy the below code;
try
{
ch = new DirectoryInfo(txtFilePath.Text);
ch.Attributes = FileAttributes.Normal;
MessageBox.Show("Visible");
}
catch { }
8. Now update your Form1.cs with the code given below.
Form1.cs
using System;
using System.IO;
using System.Windows.Forms;
namespace HideFolder
{
public partial class Form1 : Form
{
DirectoryInfo ch;
public Form1()
{
InitializeComponent();
}
private void btnBrowse_Click(object sender, EventArgs e)
{
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
txtFilePath.Text = folderBrowserDialog1.SelectedPath;
}
}
private void btnOpen_Click(object sender, EventArgs e)
{
try
{
System.Diagnostics.Process.Start(txtFilePath.Text);
}
catch { }
}
private void btnHide_Click(object sender, EventArgs e)
{
try
{
ch = new DirectoryInfo(txtFilePath.Text);
ch.Attributes = FileAttributes.Hidden;
MessageBox.Show("Hidden");
}
catch { }
}
private void btnUnhide_Click(object sender, EventArgs e)
{
try
{
ch = new DirectoryInfo(txtFilePath.Text);
ch.Attributes = FileAttributes.Normal;
MessageBox.Show("Visible");
}
catch { }
}
}
}
9. Now Run your project and Browse a folder. The folder path will be shown in the
textbox. Now click on Hide button, the folder will be Hidden and you will get a message
( see the screenshot below). Now click on Unhide to make it visible.
Conclusion
See Also
No comments:
Post a Comment