章政 5 жил өмнө
parent
commit
68fba4debd

+ 14 - 0
FileWatcher/Form1.Designer.cs

@@ -29,6 +29,7 @@
         private void InitializeComponent()
         {
             this.FileWatcher = new System.IO.FileSystemWatcher();
+            this.richTextBox1 = new System.Windows.Forms.RichTextBox();
             ((System.ComponentModel.ISupportInitialize)(this.FileWatcher)).BeginInit();
             this.SuspendLayout();
             // 
@@ -38,11 +39,23 @@
             this.FileWatcher.IncludeSubdirectories = true;
             this.FileWatcher.SynchronizingObject = this;
             // 
+            // richTextBox1
+            // 
+            this.richTextBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
+            | System.Windows.Forms.AnchorStyles.Left) 
+            | System.Windows.Forms.AnchorStyles.Right)));
+            this.richTextBox1.Location = new System.Drawing.Point(12, 12);
+            this.richTextBox1.Name = "richTextBox1";
+            this.richTextBox1.Size = new System.Drawing.Size(260, 237);
+            this.richTextBox1.TabIndex = 0;
+            this.richTextBox1.Text = "";
+            // 
             // Form1
             // 
             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
             this.ClientSize = new System.Drawing.Size(284, 261);
+            this.Controls.Add(this.richTextBox1);
             this.Name = "Form1";
             this.Text = "Form1";
             this.Load += new System.EventHandler(this.Form1_Load);
@@ -54,6 +67,7 @@
         #endregion
 
         private System.IO.FileSystemWatcher FileWatcher;
+        private System.Windows.Forms.RichTextBox richTextBox1;
     }
 }
 

+ 102 - 21
FileWatcher/Form1.cs

@@ -1,4 +1,6 @@
-using System;
+using Microsoft.Win32;
+using System;
+using System.Collections.Generic;
 using System.Diagnostics;
 using System.IO;
 using System.Windows.Forms;
@@ -14,36 +16,115 @@ namespace FileWatcher
 
         private void Form1_Load(object sender, EventArgs e)
         {
-            FileWatcher.Path = @"G:\";
+            FileWatcher.Path = @"D:\";
             FileWatcher.Filter = "*.jdf";
             FileWatcher.EnableRaisingEvents = true;
-            FileWatcher.Changed += new FileSystemEventHandler(Watcher_Created);
+            FileWatcher.Created += new FileSystemEventHandler(Watcher_Created);
+
+            string path = Application.ExecutablePath;
+            RegistryKey rk = Registry.LocalMachine;
+            RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
+            rk2.SetValue("FileWatcher.exe", path);
+            rk2.Close();
+            rk.Close();
         }
 
         private void Watcher_Created(object sender, FileSystemEventArgs e)
         {
             using (var process = new Process())
             {
-                if (!e.FullPath.Contains("RECYCLE")) {
-                    process.StartInfo.Arguments = @"D:\FileWatcher\DTS-JDFData2Excel.exe " + e.FullPath;
-                    Console.WriteLine(process.StartInfo.Arguments);
-                    process.StartInfo.FileName = @"D:\FileWatcher\DTS-JDFData2Excel.exe";
-                    process.StartInfo.UseShellExecute = false;
-                    process.StartInfo.RedirectStandardInput = true;
-                    process.StartInfo.RedirectStandardOutput = true;
-                    process.StartInfo.RedirectStandardError = true;
-                    process.StartInfo.CreateNoWindow = true;
-
-                    process.Start();
-                    process.StandardInput.AutoFlush = true;
-                    process.StandardInput.WriteLine("exit");
-                    //获取cmd窗口的输出信息  
-                    string output = process.StandardOutput.ReadToEnd();
-
-                    process.WaitForExit();
-                    process.Close();
+                if (!e.FullPath.Contains("RECYCLE"))
+                {
+                    string FullName = e.FullPath;
+                    process.StartInfo.Arguments = @"D:\FileWatcher\DTS-JDFData2Excel.exe " + FullName;
+                    string Filename = FullName.Substring(FullName.LastIndexOf(@"\") + 1).Split('.')[0];
+                    string StartPath = FullName.Substring(0, FullName.LastIndexOf(@"\") + 1);
+                    if (File.Exists(FullName))
+                    {
+                        richTextBox1.AppendText(DateTime.Now.ToString("yyyy/MM/dd h:mm:ss.fff") + e.FullPath + "\n");
+                    }
+                    else
+                    {
+                        richTextBox1.AppendText("不存在文件" + FullName + "\n");
+                    }
+                    //不存在同名的文件则进行转换
+                    if (!File.Exists(StartPath + Filename + ".xls") && File.Exists(FullName))
+                    {
+                        process.StartInfo.FileName = @"D:\FileWatcher\DTS-JDFData2Excel.exe";
+                        process.StartInfo.UseShellExecute = false;
+                        process.StartInfo.RedirectStandardInput = true;
+                        process.StartInfo.RedirectStandardOutput = true;
+                        process.StartInfo.RedirectStandardError = true;
+                        process.StartInfo.CreateNoWindow = true;
+
+                        process.Start();
+                        process.StandardInput.AutoFlush = true;
+                        process.StandardInput.WriteLine("exit");
+                        //获取cmd窗口的输出信息  
+                        string output = process.StandardOutput.ReadToEnd();
+
+                        process.WaitForExit();
+                        process.Close();
+                    }
+                }
+            }
+        }
+
+        /// <summary>
+        /// 私有变量
+        /// </summary>
+        private List<FileInfo> lst = new List<FileInfo>();
+        /// <summary>
+        /// 获得目录下所有文件或指定文件类型文件(包含所有子文件夹)
+        /// </summary>
+        /// <param name="path">文件夹路径</param>
+        /// <param name="extName">扩展名可以多个 例如 .mp3.wma.rm</param>
+        /// <returns>List<FileInfo></returns>
+        public List<FileInfo> getFile(string path, string extName)
+        {
+            getdir(path, extName);
+            return lst;
+        }
+        /// <summary>
+        /// 私有方法,递归获取指定类型文件,包含子文件夹
+        /// </summary>
+        /// <param name="path"></param>
+        /// <param name="extName"></param>
+        private void getdir(string path, string extName)
+        {
+            try
+            {
+                string[] dir = Directory.GetDirectories(path); //文件夹列表   
+                DirectoryInfo fdir = new DirectoryInfo(path);
+                FileInfo[] file = fdir.GetFiles();
+                //FileInfo[] file = Directory.GetFiles(path); //文件列表   
+                if (file.Length != 0 || dir.Length != 0) //当前目录文件或文件夹不为空                   
+                {
+                    foreach (FileInfo f in file) //显示当前目录所有文件   
+                    {
+                        if (f.Attributes.ToString().IndexOf("Hidden") > -1 && f.Attributes.ToString().IndexOf("System") > -1)
+                        {
+
+                        }
+                        else
+                        {
+                            if (extName.ToLower().IndexOf(f.Extension.ToLower()) >= 0)
+                            {
+                                lst.Add(f);
+                            }
+                        }
+                    }
+                    foreach (string d in dir)
+                    {
+                        if (d.Contains("PROB"))
+                            getdir(d, extName);//递归   
+                    }
                 }
             }
+            catch (Exception ex)
+            {
+                throw ex;
+            }
         }
     }
 }