zoukankan      html  css  js  c++  java
  • (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);

    }

    }

    }



  • 相关阅读:
    mysql不停库不锁表在线主从配置
    MySQL5.7不停机不锁表主从同步实战
    另类 k8s 集群灾难恢复方法:用 master 服务器镜像恢复出新集群
    我在新西兰卖挖掘机之续篇十八------坚持的秘诀(坚持的秘诀就2条,第一是初始摩擦力要足够小(开头不能太困难,时间不能太久);第二是坚持的事情能够不断地给你提供某种真实的好处)(其实就是正反馈,加上一点点兴趣)
    分布式系统中那些不靠谱任务-CAP 到底是什么
    分布式系统架构中关于切分数据库事务的 ACID 性质的可用性与性能-BASE 理论和CAP 定理
    SSO协议-SAML和OAuth2
    领域建模
    基于 Docker 容器Net Core微服务部署 Nginx 集群架构
    消息队列和分布式锁
  • 原文地址:https://www.cnblogs.com/johnsmith/p/2291745.html
Copyright © 2011-2022 走看看