如何使用 C# 创建、隐藏和取消隐藏文件夹?
How do I create, hide and unhide a folder using C#?
private void button1_Click(object sender, EventArgs e)
{
if (!Directory.Exists("Test"))
{
DirectoryInfo dir = Directory.CreateDirectory("Test");
dir.Attributes = FileAttributes.Directory | FileAttributes.Hidden;
}
else
{
var dir = new DirectoryInfo("Test");
dir.Attributes |= FileAttributes.Normal;
}
String password = "123";
if ((textBox1.Text == password))
{
this.Hide();
Form2 f2 = new Form2();
f2.ShowDialog();
var dir = new DirectoryInfo("Test");
dir.Attributes |= FileAttributes.Normal;
}
else
MessageBox.Show("Incorrect Password or Username", "Problem");
因此,视觉方面看起来像这样:http://prntscr.com/7rj9hc
因此,您输入密码“123”,然后单击“解锁”,理论上应该使文件夹 "test" 取消隐藏,然后您可以将内容放入其中。然后第二个表单随附一个按钮 "Lock",使文件夹再次隐藏并关闭程序,这样您就可以打开和关闭文件夹以添加更多内容并取出一些内容。那么我该怎么做呢?您拥有的任何解决方案将不胜感激,请(如果您有时间)解释每个部分在做什么。请在您的解决方案中告诉我如何再次隐藏文件夹
如果在您按下解锁按钮时执行上面的代码,则需要进行一些更改
private void button1_Click(object sender, EventArgs e)
{
if (!Directory.Exists("Test"))
{
DirectoryInfo dir = Directory.CreateDirectory("Test");
dir.Attributes = FileAttributes.Directory | FileAttributes.Hidden;
}
// NO ELSE, the folder should remain hidden until you check the
// correctness of the password
String password = "123";
if ((textBox1.Text == password))
{
// This should be moved before opening the second form
var dir = new DirectoryInfo("Test");
// To remove the Hidden attribute Negate it and apply the bit AND
dir.Attributes = dir.Attributes & ~FileAttributes.Hidden;
// A directory usually has the FileAttributes.Directory that has
// a decimal value of 16 (10000 in binary). The Hidden attribute
// has a decimal value of 2 (00010 in binary). So when your folder
// is Hidden its decimal Attribute value is 18 (10010)
// Negating (~) the Hidden attribute you get the 11101 binary
// value that coupled to the current value of Attribute gives
// 10010 & 11101 = 10000 or just FileAttributes.Directory
// Now you can hide the Unlock form, but please note
// that a call to ShowDialog blocks the execution of
// further code until you exit from the opened form
this.Hide();
Form2 f2 = new Form2();
f2.ShowDialog();
// No code will be executed here until you close the f2 instance
.....
}
else
MessageBox.Show("Incorrect Password or Username", "Problem");
要再次隐藏目录,您只需像创建目录时那样设置带有隐藏标志的属性
DirectoryInfo dir = new DirectoryInfo("Test");
dir.Attributes = FileAttributes.Directory | FileAttributes.Hidden;
终于有一个注意事项。如果您的用户是其计算机的管理员并且可以使用操作系统提供的标准界面更改隐藏文件和文件夹的属性,那么所有这些 hide/unhide 工作都是完全无用的
private void button1_Click(object sender, EventArgs e)
{
if (!Directory.Exists("Test"))
{
DirectoryInfo dir = Directory.CreateDirectory("Test");
dir.Attributes = FileAttributes.Directory | FileAttributes.Hidden;
}
else
{
var dir = new DirectoryInfo("Test");
dir.Attributes |= FileAttributes.Normal;
}
String password = "123";
if ((textBox1.Text == password))
{
this.Hide();
Form2 f2 = new Form2();
f2.ShowDialog();
var dir = new DirectoryInfo("Test");
dir.Attributes |= FileAttributes.Normal;
}
else
MessageBox.Show("Incorrect Password or Username", "Problem");
因此,视觉方面看起来像这样:http://prntscr.com/7rj9hc 因此,您输入密码“123”,然后单击“解锁”,理论上应该使文件夹 "test" 取消隐藏,然后您可以将内容放入其中。然后第二个表单随附一个按钮 "Lock",使文件夹再次隐藏并关闭程序,这样您就可以打开和关闭文件夹以添加更多内容并取出一些内容。那么我该怎么做呢?您拥有的任何解决方案将不胜感激,请(如果您有时间)解释每个部分在做什么。请在您的解决方案中告诉我如何再次隐藏文件夹
如果在您按下解锁按钮时执行上面的代码,则需要进行一些更改
private void button1_Click(object sender, EventArgs e)
{
if (!Directory.Exists("Test"))
{
DirectoryInfo dir = Directory.CreateDirectory("Test");
dir.Attributes = FileAttributes.Directory | FileAttributes.Hidden;
}
// NO ELSE, the folder should remain hidden until you check the
// correctness of the password
String password = "123";
if ((textBox1.Text == password))
{
// This should be moved before opening the second form
var dir = new DirectoryInfo("Test");
// To remove the Hidden attribute Negate it and apply the bit AND
dir.Attributes = dir.Attributes & ~FileAttributes.Hidden;
// A directory usually has the FileAttributes.Directory that has
// a decimal value of 16 (10000 in binary). The Hidden attribute
// has a decimal value of 2 (00010 in binary). So when your folder
// is Hidden its decimal Attribute value is 18 (10010)
// Negating (~) the Hidden attribute you get the 11101 binary
// value that coupled to the current value of Attribute gives
// 10010 & 11101 = 10000 or just FileAttributes.Directory
// Now you can hide the Unlock form, but please note
// that a call to ShowDialog blocks the execution of
// further code until you exit from the opened form
this.Hide();
Form2 f2 = new Form2();
f2.ShowDialog();
// No code will be executed here until you close the f2 instance
.....
}
else
MessageBox.Show("Incorrect Password or Username", "Problem");
要再次隐藏目录,您只需像创建目录时那样设置带有隐藏标志的属性
DirectoryInfo dir = new DirectoryInfo("Test");
dir.Attributes = FileAttributes.Directory | FileAttributes.Hidden;
终于有一个注意事项。如果您的用户是其计算机的管理员并且可以使用操作系统提供的标准界面更改隐藏文件和文件夹的属性,那么所有这些 hide/unhide 工作都是完全无用的