簡體   English   中英

C#為變量賦值表示無法找到文件?

[英]C# assigning value to variable says it cannot find the file?

我有一個運行批處理文件的非常簡單的方法。 方法是這樣的:

private  string _binnPath = Application.StartupPath + "\\Binn";

public void DumpFileSystem(string snapshotFolder)
{
    var cwd = Directory.GetCurrentDirectory();
    Directory.SetCurrentDirectory(_binnPath);

    var snapshotOutput = Path.Combine(Application.StartupPath, snapshotFolder);
    snapshotOutput = Path.Combine(snapshotOutput, DateTime.UtcNow.Ticks + "-files.txt");

    var batCommand = _binnPath + "\\DumpFileSystem.bat";
    string batFilename = batCommand + " " + snapshotOutput;

    using (var process = Process.Start(batFilename))
    {
        process?.WaitForExit();
    }

    Directory.SetCurrentDirectory(cwd);
}

批處理文件是這樣的:

dir /s c:\ >  %1

在一行上var batCommand = _binnPath + "\\\\DumpFileSystem.bat"; 我收到這個:

System.dll中發生了類型為'System.ComponentModel.Win32Exception'的未處理的異常

附加信息:系統找不到指定的文件

這根本沒有意義,因為我只分配了一個變量。 我要做的就是使用snapshotOutput變量運行批處理文件。 這不應該那么困難。

問題

  1. 任何想法如何使批處理文件運行?

  2. 當變量的設置甚至不應該尋找文件時,為什么會引發錯誤?

刪除這些代碼

string batFilename = batCommand + " " + snapshotOutput;

您必須更改流程開始

Process.Start(new ProcessStartInfo(batCommand , snapshotOutput))
  1. 您只應顯式傳遞批處理和參數的路徑。 否則,Windows會嘗試將參數合並到不存在的路徑中。
  2. 這是Visual Studio中的一種錯覺,通常是由較舊的文件或代碼優化引起的。

請在這里查看: https//stackoverflow.com/a/5766669/6082960

暫無
暫無

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

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