简体   繁体   中英

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

So, the visual side looks like this: http://prntscr.com/7rj9hc So you type in the password "123" and then click unlock which should in theory make the folder "test" unhide and you can put stuff in. Then a second form comes along with a button "Lock" which makes the folder hide again and closes the program so you can then open and close the folder to add more stuff and take some out. So how would i go about doing this? Any solutions you have will be much appreciated and please (if you have time) explain what it is each part is doing. Please in your solution can you show me how to hide the folder again

If the code above is executed when you press the Unlock button then a couple of changes are required

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

To hide the directory again you need to simply set the Attributes with the Hidden flag as you have done when you created the directory

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

Finally a note. All this hide/unhide work is totally useless if your user is an Administrator of its machine and can change the Attributes of the Hidden file and Folders using the standard interface provided by the operating system

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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