【问题标题】:c# automation stop processc#自动化停止进程
【发布时间】:2020-12-18 22:21:52
【问题描述】:

我有启动进程的 C# 自动化代码

 var proc1 = new ProcessStartInfo();

    string anyCommand = " adb logcat - v threadtime emulator-5554 > logcat.log";
    proc1.UseShellExecute = true;

    proc1.WorkingDirectory = outputDirectory;

    proc1.FileName = @"C:\Windows\System32\cmd.exe";
    proc1.Verb = "runas";
    proc1.Arguments = "/c " + anyCommand;
    proc1.WindowStyle = ProcessWindowStyle.Hidden;
    Process p = new Process();
    p.StartInfo = proc1;
    p.Start();
    Console.WriteLine(p.Id);
    TestLogger.WriteInformationStep("p.Id: " + p.Id);

经过一些步骤后,我正在尝试读取文件

        string text2 = System.IO.File.ReadAllText(element);

但我收到错误消息

System.IO.IOException : 进程无法访问文件 'C:\Users\test\Documents\overview9\bin\Debug\Results\logcat.log' 因为它正在被另一个进程使用。拆除 : HarVE.Log.FailedStepException:有 1 个失败的步骤:测试完成 没有运行完成

我该怎么办? 我试过p.Close();p.Kill(); 它们都不适合我。

【问题讨论】:

    标签: c# powershell automation process


    【解决方案1】:

    如果 p.Kill();放置在 System.IO.File.ReadAllText(element); 之后的位置在 System.IO.File.ReadAllText(element) 之前写 p.Kill(), 如果它放置正确,那么似乎其他进程正在读取/写入该文件, 尝试使用 System.IO 中的 StreamReader,StreamReader 可以多次读取,如下所示:

     StreamReader sr = new StreamReader(path_to_file);//replace path_to_file by your txt file path
     string text2 = sr.ReadToEnd();
    

    【讨论】:

      猜你喜欢
      • 2020-05-29
      • 1970-01-01
      • 2018-10-23
      • 1970-01-01
      • 1970-01-01
      • 2020-08-09
      • 2011-12-14
      • 2020-01-12
      • 1970-01-01
      相关资源
      最近更新 更多