简体   繁体   中英

Hiding Directories Programmatically in C#

I want to make a directory hidden in Windows Vista. Not hidden completely just from view. Like you set from the folder options. I tried something along the lines of an example I saw. Only I modified it slightly..

Here is all of my code combined.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class hideme : Form
    {
        public hideme()
        {
            InitializeComponent();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (PasswordTextBox.Text == "test")
            {
                EnableButton.Visible = true;
                DisableButton.Visible = true;
            }
            else
            {
                MessageBox.Show("Wrong", "Attention");
                Application.Exit();
            }
        }


        private void EnableButton_Click(object sender, EventArgs e)
        {
            //System.IO.FileInfo dir = new System.IO.FileInfo("C:\\Users\\logickills\\Pictures\\system");
            string path = "C:\\Users\\chris\\Pictures\\system";
            FileInfo FIh1 = new FileInfo(Environment.CurrentDirectory + @"\Files\File2.txt");
            FIh1.Attributes = FileAttributes.Hidden;
        }

        private void DisableButton_Click(object sender, EventArgs e)
        {

        }

        private void PasswordTextBox_TextChanged(object sender, EventArgs e)
        {

        }
    }
}

This goes along with the dialog I was creating earlier here . The two buttons that are shown after password is entered is for showing and hiding that directory.

The Attribute property is a combination of attributes, so you will need to combine the Hidden attribute with whatever attributes the item already has got:

FIh1.Attributes = FIh1.Attributes  | System.IO.FileAttributes.Hidden;

If you want to remove it you can use the following code:

if ((FIh1.Attributes & System.IO.FileAttributes.Hidden) == System.IO.FileAttributes.Hidden)
{
    FIh1.Attributes = FIh1.Attributes ^ System.IO.FileAttributes.Hidden;
}

If you call FIh1.Attributes = FIh1.Attributes ^ System.IO.FileAttributes.Hidden; repeatedly you will toggle the hidden attribute on and off every second time.

Here is the code to make it hidden (make it totally invisible) and show again.

This is to hide the folder:

string path = @"c:\folders\"; 
DirectoryInfo di = Directory.CreateDirectory(path);
di.Attributes = FileAttributes.Directory | FileAttributes.Hidden;           
MessageBox.Show("archivo escondido");

This is to show the folder again

string path = @"c:\folders\"; 
DirectoryInfo di = Directory.CreateDirectory(path);
di.Attributes = FileAttributes.Directory | FileAttributes.Normal;
MessageBox.Show("archivo mostrado");

You are retrieving the attributes, not saving those changes ever.

use this to set them

            File.SetAttributes(path, FileAttributes.Hidden);

Well, a question very similar to this one was asked on my Group recently.

The DirectoryInfo class exposes an "Attributes" property which is a Flags enumeration on the FileAttributes enumeration. You should note that a Directory is basically a file with a special attribute - "Directory". Therefore, you should not clear that attribute, rather you should always append to the existing enumerated value or remove from it.

Here's a very simple Console app to demonstrate the concept:


class Program 
{ 
  static void Main(string[] args) 
  { 
    string input = args[0]; 
    if(input == "hide") 
    { 
      ToggleHidden(true); 
    } 
    else if(input == "reveal") 
    { 
      ToggleHidden(false); 
    } 
  } 


  private static void ToggleHidden(bool hide) 
  { 
    DirectoryInfo d = new DirectoryInfo(@"C:\MySecretFolder"); 
    if(d.Exists) 
    { 
      FileAttributes atts = d.Attributes; 
      if(hide == true) 
      { // Hide the folder. 
        // Append Hidden attribute only if not already set. 
        if((atts & FileAttributes.Hidden) != FileAttributes.Hidden) 
          atts |= FileAttributes.Hidden; 
      } 
      else 
      {  // Show the folder. 
        // Remove Hidden attribute if set. 
        if((atts & FileAttributes.Hidden) == FileAttributes.Hidden) 
          atts &= ~FileAttributes.Hidden; 
      } 


      d.Attributes = atts; 
    } 
  } 

} 

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