直接用控件,代码如下:
public
static
FileSystemWatcher watcher =
new
FileSystemWatcher();
|
接下来就是为对watcher 的事件捕获并进行相应处理,代码如下:
watcher.Changed +=
new
FileSystemEventHandler(OnChanged);
watcher.Created +=
new
FileSystemEventHandler(OnChanged);
watcher.Deleted +=
new
FileSystemEventHandler(OnChanged);
watcher.Renamed +=
new
RenamedEventHandler(OnRenamed);
|
加入下面的代码,能有效地避免程序被误认为是非法操作。
[PermissionSetAttribute(SecurityAction.Demand, Name
=
"
FullTrust
"
)]
开机启动
private
void
AutoRun_CheckedChanged(
object
sender, EventArgs e)
{
if
(AutoRun.Checked)
//开机自动启动
{
try
{
RegistryKey runKey = Registry.LocalMachine.OpenSubKey(
@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run"
,
true
);
runKey.SetValue(
"文件夹监测.exe"
, System.Windows.Forms.Application.ExecutablePath);
runKey.Close();
}
catch
(IOException)
{
return
;
}
}
else
//不开机自动启动注册表信息删除
{
RegistryKey software = Registry.LocalMachine.OpenSubKey(
@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run"
,
true
);
string
[] aimnames = software.GetValueNames();
foreach
(
string
aimKey
in
aimnames)
{
if
(aimKey.Equals(
"文件夹监测.exe"
))
{
software.DeleteValue(
"文件夹监测.exe"
) ;
software.Close();
break
;
}
}
}
}
|