Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Visual C#-运行程序时,如何将文件从资源复制到磁盘中的文件夹?_C#_Process_Resources_Backgroundworker - Fatal编程技术网

Visual C#-运行程序时,如何将文件从资源复制到磁盘中的文件夹?

Visual C#-运行程序时,如何将文件从资源复制到磁盘中的文件夹?,c#,process,resources,backgroundworker,C#,Process,Resources,Backgroundworker,在Visual C#中创建一个简单的应用程序时遇到问题,该应用程序执行以下操作: 坚持寻找一个特定的过程 当该进程开始运行时,将资源复制到文件夹中 当进程结束时,将另一个资源复制到该文件夹 我尝试使用计时器+线程,但没有成功。我认为一个后台工作人员会在这方面提供帮助,但文档并不清楚。这是我目前的代码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Diagnost

在Visual C#中创建一个简单的应用程序时遇到问题,该应用程序执行以下操作:

  • 坚持寻找一个特定的过程
  • 当该进程开始运行时,将资源复制到文件夹中
  • 当进程结束时,将另一个资源复制到该文件夹
  • 我尝试使用计时器+线程,但没有成功。我认为一个后台工作人员会在这方面提供帮助,但文档并不清楚。这是我目前的代码:

    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);
    
            }
        }
    }
    

    正如我在评论中提到的,您当前的代码只运行一次检查,然后BackgroundWorker就完成了。你可以:

    • 使用计时器,按照当前在
      循环中的代码行,间歇性地(比如每分钟)查找进程。流程启动后,您可以使用
      process.WaitForExit()
      暂停代码,直到流程完成。要使您的程序继续响应,此部分应位于
      BackgroundWorker
      中。当然,这种方法存在一种风险,即进程在计时器计时之间快速启动和完成

    • 在WMI中使用
      ManagementEventWatcher
      。关于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();
    

    正在发生的情况、任何错误或功能不起作用当前您的后台工作程序只运行一个,而不是其名称所暗示的循环。我现在将尝试此操作,并在适当时将您的答案标记为正确。它起作用了!有点。。。当我关闭程序时,调试器中会出现一个错误/警告:已与其基础RCW分离的COM对象无法使用。