簡體   English   中英

c#自動停止進程

[英]c# automation stop process

我有啟動進程的 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(); 他們都不為我工作。

如果 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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM