在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.
再次感谢SO。
(对不起这么多帖子我想完成这个程序: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屋!