tags:

views:

327

answers:

3

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.

Thanks again SO.

(Sorry for so many posts I want to finish this program :D)

+2  A: 

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.

Fredrik Mörk
It says i may be missing a namespace
Added namespaces to the type names. One other option is to put "using System.IO;" at the top of the code file.
Fredrik Mörk
It still wont hide the directory. Everything is recognized now though.
nevermind I have it :)Thank you all
+1  A: 

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

use this to set them

            File.SetAttributes(path, FileAttributes.Hidden);
Tetraneutron
A: 

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; 
    } 
  } 

}
Cerebrus
Please note - `System.IO` namespace to be imported in the above code sample.
Cerebrus