资源简介
自动修改当前目录下所有文件的属性为隐藏并去掉只读属性
项目生成时好用...
代码片段和文件信息
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;
using System.IO;
namespace WatchBin
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender EventArgs e)
{
FilterType = txtFilterType.Text; //防止跨越线程访问异常
string path = Application.StartupPath;
//**********************************************//
//1.刚开始启动程序就把所有的文件全部都检测一边.
//**********************************************//
//string[] dir = Directory.GetDirectories(path); //文件夹列表
//DirectoryInfo fdir = new DirectoryInfo(path);
//FileInfo[] file = fdir.GetFiles();
string[] files = Directory.GetFiles(path); //文件列表
foreach (string filefullpath in files)
{
ChangAttribute(filefullpath);
}
//**********************************************//
//2.开始监视.
//**********************************************//
FileSystemWatcher watcher = new FileSystemWatcher(path “*.*“);
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName;
// Only watch text files.
// Add event handlers.
watcher.Changed += new FileSystemEventHandler(w_Changed);
watcher.Created += new FileSystemEventHandler(w_Changed);
// Begin watching.
watcher.EnableRaisingEvents = true;
}
string FilterType = ““;
void w_Changed(object sender FileSystemEventArgs e)
{
try
{
string ext = e.FullPath.Substring(e.FullPath.LastIndexOf(‘.‘));
if (FilterType.Contains(ext))
{
if (e.ChangeType != WatcherChangeTypes.Deleted)
{
ChangAttribute(e.FullPath);
}
}
}
catch (Exception ex)
{
MessageBox.Show(“监视“ + e.FullPath + e.ChangeType.ToString() + “ error Message “ + ex.Message);
}
}
///
/// 去掉指定文件的只读属性并且加上隐藏属性
///
///
private static void ChangAttribute(string fullpath)
{
FileAttributes attributes = File.GetAttributes(fullpath);
//去掉只读属性
if ((attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
{
// Show the file.
attributes = RemoveAttribut
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2012-05-26 17:03 WatchBin\
文件 3554 2012-05-26 17:05 WatchBin\Form1.Designer.cs
文件 3728 2012-05-26 17:05 WatchBin\Form1.cs
文件 5817 2012-05-26 17:05 WatchBin\Form1.resx
文件 489 2012-05-26 12:23 WatchBin\Program.cs
目录 0 2012-05-26 17:03 WatchBin\Properties\
文件 1352 2012-05-26 12:23 WatchBin\Properties\AssemblyInfo.cs
文件 2864 2012-05-26 12:23 WatchBin\Properties\Resources.Designer.cs
文件 5612 2012-05-26 12:23 WatchBin\Properties\Resources.resx
文件 1091 2012-05-26 12:23 WatchBin\Properties\Settings.Designer.cs
文件 249 2012-05-26 12:23 WatchBin\Properties\Settings.settings
文件 247 2012-05-26 17:03 WatchBin\Properties\vssver2.scc
文件 3882 2012-05-26 17:03 WatchBin\WatchBin.csproj
文件 274 2012-05-26 17:03 WatchBin\WatchBin.csproj.vspscc
目录 0 2012-05-26 12:23 WatchBin\bin\
目录 0 2012-05-26 12:23 WatchBin\bin\Debug\
文件 8704 2012-05-26 12:37 WatchBin\bin\Debug\WatchBin.exe
文件 22016 2012-05-26 12:37 WatchBin\bin\Debug\WatchBin.pdb
文件 173 2012-05-26 17:03 WatchBin\mssccprj.scc
目录 0 2012-05-26 12:23 WatchBin\obj\
目录 0 2012-05-26 12:23 WatchBin\obj\x86\
目录 0 2012-05-26 13:06 WatchBin\obj\x86\Debug\
文件 4440 2012-05-26 13:24 WatchBin\obj\x86\Debug\DesignTimeResolveAssemblyReferences.cache
文件 6256 2012-05-26 17:01 WatchBin\obj\x86\Debug\DesignTimeResolveAssemblyReferencesInput.cache
文件 272 2012-05-26 13:06 WatchBin\obj\x86\Debug\GenerateResource.read.1.tlog
文件 646 2012-05-26 13:06 WatchBin\obj\x86\Debug\GenerateResource.write.1.tlog
目录 0 2012-05-26 12:23 WatchBin\obj\x86\Debug\TempPE\
文件 180 2012-05-26 13:06 WatchBin\obj\x86\Debug\WatchBin.Form1.resources
文件 180 2012-05-26 13:06 WatchBin\obj\x86\Debug\WatchBin.Properties.Resources.resources
文件 975 2012-05-26 13:54 WatchBin\obj\x86\Debug\WatchBin.csproj.FileListAbsolute.txt
文件 10240 2012-05-26 13:06 WatchBin\obj\x86\Debug\WatchBin.exe
............此处省略2个文件信息
评论
共有 条评论