C# 用C语言编程隐藏目录#
我想在Windows Vista中隐藏一个目录。不是完全隐藏在视线之外。就像您从文件夹选项中设置的一样。 我试着按照我看到的一个例子来做。只是我稍微修改了一下 下面是我所有的代码组合C# 用C语言编程隐藏目录#,c#,directory,C#,Directory,我想在Windows Vista中隐藏一个目录。不是完全隐藏在视线之外。就像您从文件夹选项中设置的一样。 我试着按照我看到的一个例子来做。只是我稍微修改了一下 下面是我所有的代码组合 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; us
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)
{
}
}
}
这与我之前创建的对话框是一致的。
输入密码后显示的两个按钮用于显示和隐藏该目录。属性是属性的组合,因此您需要将隐藏属性与项目已获得的任何属性组合:
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代码>重复,您将每秒打开和关闭隐藏属性。您正在检索属性,而不是保存这些更改
用这个来设置它们
File.SetAttributes(path, FileAttributes.Hidden);
最近有人问了一个非常类似的问题
DirectoryInfo类公开一个“Attributes”属性,该属性是FileAttributes枚举上的标志枚举。您应该注意,目录基本上是一个具有特殊属性“Directory”的文件。因此,您不应该清除该属性,而应该
您应该始终附加到现有枚举值或从中删除
下面是一个非常简单的控制台应用程序来演示这一概念:
下面是将其隐藏(使其完全不可见)并再次显示的代码
这是为了隐藏文件夹:
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");
你能更清楚地说明你的问题吗?你的例子不管用吗?我不知道隐藏文件夹有多实用!你是说“按程序”吗?马特:我也很奇怪:p@matt:你太好笑了!我现在是ROFL,它说我可能缺少一个名称空间,在类型名中添加了名称空间。另一种选择是将“using System.IO;”放在代码文件的顶部。它仍然不会隐藏目录。不过现在一切都已识别。您也可以使用|=运算符(FIh1.Attributes |=System.IO.FileAttributes.Hidden;
)请注意-System.IO
命名空间将导入上述代码示例中。
string path = @"c:\folders\";
DirectoryInfo di = Directory.CreateDirectory(path);
di.Attributes = FileAttributes.Directory | FileAttributes.Normal;
MessageBox.Show("archivo mostrado");