在C#中以编程方式隐藏目录 [英] Hiding Directories Programmatically in C#
问题描述
我想在Windows Vista中隐藏目录。并非完全隐藏不可见。就像您从文件夹选项中设置的一样。
我按照所看到的示例进行了尝试。
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.
推荐答案
Attribute属性是属性的 combination ,因此您需要将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;
}
如果您调用 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.
这篇关于在C#中以编程方式隐藏目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!