2012-07-04 28 views
0

我無法創建在Visual C#一個簡單的應用程序,即執行以下操作:Visual C# - 當程序運行時如何將資源中的文件複製到磁盤中的文件夾?

  1. 大聲疾呼的固執尋求特定的進程。
  2. 當該進程開始運行時,將資源複製到文件夾。
  3. 當過程結束時,將另一個資源複製到該文件夾​​。

我試過使用定時器+線程,沒有成功。我認爲BackgroundWorker在這裏會有所幫助,但文檔相當不清楚。這是我當前的代碼:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Diagnostics; 
using System.IO; 
using System.Linq; 
using System.Resources; 
using System.Text; 
using System.Threading; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Navigation; 
using System.Windows.Shapes; 

namespace ResourceCopyApp 
{ 
    public partial class MainWindow : Window { 
     BackgroundWorker bw; 
     bool pcRunning = false; 
     public MainWindow() { 
      InitializeComponent(); 
      bw = new BackgroundWorker(); 
      bw.DoWork += new DoWorkEventHandler(Loop); 
      bw.RunWorkerAsync(); 
     } 

     private void Window_Loaded(object sender, RoutedEventArgs e) { 
      cfgInstall.Text = Properties.Settings.Default.pcLocation; 
      cfgLag.Value = Properties.Settings.Default.lag; 
     } 

     private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) { 
      Properties.Settings.Default.pcLocation = cfgInstall.Text; 
      Properties.Settings.Default.lag = (int)cfgLag.Value; 
      Properties.Settings.Default.Save(); 
     } 

     public void Loop(object sender, DoWorkEventArgs e) { 
      pcRunning = false; 
      bool pcWasRunning = pcRunning; 
      if (pcRunning && !pcWasRunning) { 
       MessageBox.Show("Process found"); 
       pcWasRunning = true; 
       Swap(true); 
      } 
      foreach (Process p in Process.GetProcesses()) { 
       if (p.ProcessName.Equals("Process")) { 
        pcRunning = true; 
       } 
      } 
      if (!pcRunning && pcWasRunning) { 
       MessageBox.Show("Process lost"); 
       Swap(false); 
      } 
     } 
     public void Swap(bool v) { 
      byte[] file; 
      Thread.Sleep(new TimeSpan((long)cfgLag.Value)); 
      if (v) { 
       file = Properties.Resources.NewRes; 
      } else { 
       file = Properties.Resources.BackupRes; 
      } 
      string path = cfgInstall.Text; 
      if (!cfgInstall.Text.EndsWith("/") && !cfgInstall.Text.EndsWith("\\")) { 
       path = cfgInstall.Text + "/test.txt"; 
      } 
      File.WriteAllBytes(path, file); 

     } 
    } 
} 
+0

發生了什麼,任何錯誤或哪些功能無法正常工作 –

+1

當前,您的後臺工作人員只能運行一個而不是循環,因爲它的名稱會隱含。 – Michael

回答

1

正如我在評論中提到的,您當前的代碼只運行一次檢查,然後完成BackgroundWorker。你既可以:

  • 使用定時器來尋找間歇過程(每分鐘說),沿您目前在Loop的代碼行。當過程開始時,您可以使用Process.WaitForExit()來暫停您的代碼,直到過程結束。要繼續讓你的程序響應,這部分應該在BackgroundWorker。當然,這種方法存在一個風險,即在計時器滴答之間,流程開始並迅速結束。在WMI中使用ManagementEventWatcherThis文章在CodeProject定義要通知簡化了使用WMI類當一個進程啓動和停止:

    notePad = new ProcessInfo("notepad.exe"); 
    notePad.Started += 
        new Win32Process.ProcessInfo.StartedEventHandler(this.NotepadStarted); 
    notePad.Terminated += 
        new Win32Process.ProcessInfo.TerminatedEventHandler(this.NotepadTerminated); 
    

他們所使用的底層代碼如下:

string queryString = 
    "SELECT *" + 
    " FROM __InstanceOperationEvent " + 
    "WITHIN " + pol + 
    " WHERE TargetInstance ISA 'Win32_Process' " + 
    " AND TargetInstance.Name = '" + appName + "'"; 

    // You could replace the dot by a machine name to watch to that machine 
    string scope = @"\\.\root\CIMV2"; 

    // create the watcher and start to listen 
    watcher = new ManagementEventWatcher(scope, queryString); 
    watcher.EventArrived += 
      new EventArrivedEventHandler(this.OnEventArrived); 
    watcher.Start(); 
+0

我會立即嘗試,並在適當的時候將您的答案標記爲正確。 – Kroltan

+0

它的工作!有點...當我關閉程序時,在調試器中出現錯誤/警告:無法使用與其基礎RCW分離的COM對象。 – Kroltan

+0

但爲了這個問題的目的,它的工作。 – Kroltan

相關問題