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”,然后点击解锁,这理论上应该使文件夹“测试”取消隐藏,你可以放入东西。然后第二个表格带有一个“锁定”按钮,使文件夹再次隐藏,关闭程序,然后您可以打开和关闭该文件夹以添加更多的东西,并取出一些。那我该怎么做呢?您将获得任何解决方案,并且(如果您有时间)请解释每个部分正在做什么。请在您的解决方案中,您可以告诉我如何再次隐藏文件夹
答案 0 :(得分:1)
如果按下解锁按钮时执行上述代码,则需要进行一些更改
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;
最后一个说明。如果您的用户是其计算机的管理员并且可以使用操作系统提供的标准接口更改隐藏文件和文件夹的属性,则所有这些隐藏/取消隐藏工作都是无用的