在C#中隐藏目录编程方式 [英] Hiding Directories Programatically 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.
再次感谢左右。
(对不起那么多帖子我要完成此程序:D)
(Sorry for so many posts I want to finish this program :D)
推荐答案
属性属性一个的组合的属性,所以你需要将隐藏属性与任何属性的项目已经得到了结合:
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屋!