如何使用C#创建,隐藏和取消隐藏文件夹?

图集7
    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;

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用Java隐藏文件夹

.bat和robocopy错误-隐藏文件夹

使用Java在Windows上隐藏文件/文件夹

如何隐藏desktop.ini文件而不隐藏其他隐藏文件/文件夹?

绝对隐藏文件或文件夹

如何从链接中隐藏文件夹名称

如何在Android应用程序中创建隐藏文件夹?

如何在Windows 10中使用密码保护和隐藏文件夹内容?

.gitignore 中的隐藏文件夹

如何在NSDocumentsDirectory中隐藏文件夹并禁止通过iTunes和iCloud进行备份

如何通过使用终端或脚本隐藏文件夹中的所有文件

使用robots.txt从搜索引擎隐藏文件夹和子文件夹的正确语法

使用带有子文件夹的.htaccess隐藏文件夹

如何在Android SD卡中创建隐藏文件夹并将内容推送到其中

使用Visual Basic取消隐藏所有隐藏的文件,文件夹,子文件夹和子文件,但拒绝访问的路径除外

取消隐藏osx中的文件夹

如何使用 Alt 键隐藏/取消隐藏文件管理器 PCmanFM 的菜单栏?

PowerShell脚本从隐藏文件夹中删除文件

删除文件夹中的隐藏文件

如何在Nautilus中获取文件夹大小以包含隐藏文件(点文件)?

AppleScript:如何获取文件夹中没有隐藏文件的文件?

即使显示所有文件,如何完全隐藏文件/文件夹?

我可以为通用Windows Phone应用程序创建隐藏文件夹吗?

使用名称中包含空格的隐藏文件和文件\文件夹

如何使用命令提示符在Windows7中隐藏文件夹?

OSX上的Avast:如何添加隐藏文件和文件夹作为例外?

如何在Windows中深深隐藏文件/文件夹

如何从受密码保护的RAR文件中隐藏文件夹结构?

如何在Nautilus中隐藏文件和文件夹?