【问题标题】:How do I create, hide and unhide a folder using C#?如何使用 C# 创建、隐藏和取消隐藏文件夹?
【发布时间】:2015-09-30 09:30:26
【问题描述】:
    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”,然后单击解锁,理论上应该使文件夹“测试”取消隐藏,您可以放入东西。然后第二个表单带有一个按钮“锁定”,它使文件夹再次隐藏并且关闭程序,然后您可以打开和关闭文件夹以添加更多内容并取出一些内容。那么我该怎么做呢?您拥有的任何解决方案将不胜感激,请(如果您有时间)解释每个部分在做什么。请在您的解决方案中告诉我如何再次隐藏文件夹

【问题讨论】:

    标签: c# hide directory


    【解决方案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;
    

    最后一个注释。如果您的用户是其机器的管理员并且可以使用操作系统提供的标准界面更改隐藏文件和文件夹的属性,那么所有这些隐藏/取消隐藏工作都是完全没用的

    【讨论】:

    • 它创建了“Test”文件夹,但它似乎被卡在了隐藏状态,即使输入了正确的密码,程序也不会使文件夹可见。这可能只是我的计算机而不是您的代码,您对原因有什么建议吗?谢谢你的回复和解释,真的帮助我理解了。
    • 如何检查文件夹是否可见?您使用文件资源管理器吗?如果是,您是否更改到其他文件夹,然后重试返回到之前的文件夹以检查是否已执行更新?
    • 我第一次运行程序时将“不显示隐藏文件夹”勾选为是,但当我输入密码时该文件夹没有显示。因此将其更改为“显示隐藏文件夹”并删除了该文件夹,而不是第一次运行创建并重新开始。它创建了该文件夹,但在输入正确的密码时它不可见。
    • @Atlas7 我已经用代码和注释更新了答案,解释了属性操作背后的逻辑
    • 像魅力一样工作,非常感谢您的所有帮助和解释。没有多少人解释每个步骤的作用,但你做了,谢谢你,一切顺利。
    猜你喜欢
    • 1970-01-01
    • 2018-10-18
    • 1970-01-01
    • 1970-01-01
    • 2012-08-03
    • 1970-01-01
    • 1970-01-01
    • 2016-09-27
    • 1970-01-01
    相关资源
    最近更新 更多