(WinForm)文件夹状态监控,最小化到托盘,开机自启动

  1.     文件夾監控(監測文件夾中的文件動態):

    //MSDN上的例子
public class Watcher

{



public static void Main()

{

Run();



}



[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]

public static void Run()

{

string[] args = System.Environment.GetCommandLineArgs();



// If a directory is not specified, exit program.

if (args.Length != 2)

{

// Display the proper way to call the program.

Console.WriteLine("Usage: Watcher.exe (directory)");

return;

}



// Create a new FileSystemWatcher and set its properties.

FileSystemWatcher watcher = new FileSystemWatcher();

watcher.Path = args[1];

/* Watch for changes in LastAccess and LastWrite times, and

the renaming of files or directories.
*/

watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite

| NotifyFilters.FileName | NotifyFilters.DirectoryName;

// Only watch text files.

watcher.Filter = "*.txt";



// Add event handlers.

watcher.Changed += new FileSystemEventHandler(OnChanged);

watcher.Created += new FileSystemEventHandler(OnChanged);

watcher.Deleted += new FileSystemEventHandler(OnChanged);

watcher.Renamed += new RenamedEventHandler(OnRenamed);



// Begin watching.

watcher.EnableRaisingEvents = true;



// Wait for the user to quit the program.

Console.WriteLine("Press \'q\' to quit the sample.");

while (Console.Read() != 'q') ;

}



// Define the event handlers.

private static void OnChanged(object source, FileSystemEventArgs e)

{

// Specify what is done when a file is changed, created, or deleted.

Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);

}



private static void OnRenamed(object source, RenamedEventArgs e)

{

// Specify what is done when a file is renamed.

Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath, e.FullPath);

}

}

 

  2.     最小化到托盤功能:

首先給主界面添加一個notifyIcon控件,給它的Icon添加一個圖標,不添加圖標的話不會在托盤顯示,然後給主界面的SizeChanged事件注冊一個方法,在方法中將主界面隱藏:

 

        void frmMain_SizeChanged(object sender, EventArgs e)

{

if (this.WindowState == FormWindowState.Minimized) {

this.Hide();

}

}

然後給notifyIcon的單擊或者雙擊事件添加一個方法,讓鼠標單擊或者雙擊托盤圖標的時候可以將主界面窗口還原:

 

        private void notifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)

{

this.Visible = true;

this.WindowState = FormWindowState.Normal;

}

3.讓程序開機自動啟動的方法(編輯注冊表)

        private void chbStartup_CheckedChanged(object sender, EventArgs e)

{

if (this.chbStartup.Checked)

{

try

{

string startupPath = Application.ExecutablePath;

RegistryKey local = Registry.LocalMachine;

RegistryKey run = local.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");

run.SetValue("FolderWatcher", startupPath);

local.Close();

}

catch (Exception ex)

{

MessageBox.Show("開機啟動設置異常:" + ex.Message);

}

}

else {

try

{

string startupPath = Application.ExecutablePath;

RegistryKey local = Registry.LocalMachine;

RegistryKey run = local.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");

run.DeleteValue("FolderWatcher");

local.Close();

}

catch (Exception ex)

{

MessageBox.Show("開機啟動設置異常:" + ex.Message);

}

}

}



posted on 2011-12-18 13:57  啊風  阅读(738)  评论(0编辑  收藏  举报