繁体   English   中英

如何使用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”取消隐藏,然后您可以放入东西。第二种形式带有“锁定”按钮,该按钮使文件夹再次隐藏并关闭程序,因此您可以打开和关闭文件夹以添加更多内容并取出其中的一些内容。 那我该怎么做呢? 您所拥有的任何解决方案将不胜感激,请(如果您有时间)说明每个零件在做什么。 请在您的解决方案中告诉我如何再次隐藏文件夹

如果在按下“解锁”按钮时执行了上面的代码,则需要进行一些更改

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");

要再次隐藏目录,您只需像创建目录时所做的那样简单地使用Hidden标志设置属性

   DirectoryInfo dir = new DirectoryInfo("Test");
   dir.Attributes = FileAttributes.Directory | FileAttributes.Hidden;

最后一个音符。 如果您的用户是计算机的管理员,并且可以使用操作系统提供的标准界面来更改“隐藏文件”和“文件夹”的属性,则所有这些隐藏/取消隐藏工作完全没有用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM