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

发布于 2024-07-24 20:57:00 字数 1632 浏览 5 评论 0原文

我想在 Windows Vista 中隐藏一个目录。 只是从视图中没有完全隐藏。 就像您从文件夹选项中设置的那样。 我尝试了一些与我看到的例子类似的东西。 只是我稍微修改了它。

这是我所有的代码组合。

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)
        {

        }
    }
}

这与我之前在此处创建的对话框一致。 输入密码后显示的两个按钮用于显示和隐藏该目录。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

被你宠の有点坏 2024-07-31 20:57:00

Attribute 属性是属性的组合,因此您需要将 Hidden 属性与该项目已有的任何属性组合起来:

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

如果您想删除它,您可以使用以下代码:

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

如果您调用FIh1.Attributes = FIh1.Attributes ^ System.IO.FileAttributes.Hidden; 重复地,您将每秒打开和关闭隐藏属性。

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.

风筝有风,海豚有海 2024-07-31 20:57:00

您正在检索属性,而不是保存这些更改。

用这个来设置它们

            File.SetAttributes(path, FileAttributes.Hidden);

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

use this to set them

            File.SetAttributes(path, FileAttributes.Hidden);
马蹄踏│碎落叶 2024-07-31 20:57:00

这是使其隐藏(使其完全不可见)并再次显示的代码。

这是隐藏文件夹:

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

这是再次显示文件夹

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

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");
柏林苍穹下 2024-07-31 20:57:00

嗯,最近在我的论坛上提出了一个与此非常相似的问题。

DirectoryInfo 类公开“Attributes”属性,该属性是 FileAttributes 枚举上的 Flags 枚举。 您应该注意,目录基本上是一个具有特殊属性的文件 - “目录”。 因此,您不应该清除该属性,而是
您应该始终附加到现有的枚举值或从中删除。

这是一个非常简单的控制台应用程序来演示这个概念:


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

} 

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

} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文