简体   繁体   English

在 C# 中以编程方式隐藏目录

[英]Hiding Directories Programmatically in C#

I want to make a directory hidden in Windows Vista.我想在 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: Attribute 属性是属性的组合,因此您需要将 Hidden 属性与该项目已有的任何属性相结合:

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;如果调用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. DirectoryInfo 类公开了一个“Attributes”属性,它是 FileAttributes 枚举上的 Flags 枚举。 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; 
    } 
  } 

} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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