用 FileSystemWatcher监视文件夹, e.FullPath返回文件发生变化的路径加文件名,再用e.FullPath.Substring(e.FullPath.Length -10,10).Substring(e.FullPath.Length -10,10)分离出文件名,用if语句判断文件名是否相等。
 if (e.FullPath.Substring(e.FullPath.Length -10,10) ==textBox16.Text)
            {
                MessageBox.Show(e.FullPath);
            }在窗体textBox16的text中是“999999.TXT”,上面的if语句在执行时立即程序窗体消失,visual studio 连错都不报,程序就不在了。
但下面语句是可以正确执行的,将textBox16.Text直接写成“999999.TXT” if (e.FullPath.Substring(e.FullPath.Length -10,10) ==“999999.TXT”)
            {
                MessageBox.Show(e.FullPath);
            }
请问如何做变正确用==textBox16.Text

解决方案 »

  1.   

    System.IO.Path 提供了很多静态方法用来操作路径字符串...
      

  2.   

    下面语句是此节的全部程序
                FileSystemWatcher watcher = new FileSystemWatcher();
                watcher.Path = "E:\aaa";
                watcher.Changed += new FileSystemEventHandler(watcher_Changed);
                watcher.EnableRaisingEvents = true;
            private void watcher_Changed(object sender, FileSystemEventArgs e)
            {
                //要将e.FullPath分开,直接与bbb.txt比较            if (e.FullPath.Substring(e.FullPath.Length -10,10) =="999999.TXT")
                {
                    MessageBox.Show(e.FullPath);
                }
                else
                {
                    MessageBox.Show("555");
                }
            } 
      

  3.   

    不好意思,再补充一下,此程序是wpf程序
      

  4.   

    c#FORM为控件自动生成很多代码,
        private System.IO.FileSystemWatcher fileSystemWatcher1
    窗体设计器生成的代码
            this.fileSystemWatcher1 = new System.IO.FileSystemWatcher();
       
                ((System.ComponentModel.ISupportInitialize)(this.fileSystemWatcher1)).BeginInit();
                // 
                this.fileSystemWatcher1.EnableRaisingEvents = true;
                this.fileSystemWatcher1.SynchronizingObject = this;
                this.fileSystemWatcher1.Changed += new System.IO.FileSystemEventHandler(this.fileSystemWatcher1_Changed);
    可能fileSystemWatcher并不能在wpf下用。