C# 给变量赋值说找不到文件?

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

我有一个非常简单的方法,运行s 一个批处理文件。方法是这样的:

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"; 我收到了这个:

An unhandled exception of type 'System.ComponentModel.Win32Exception' occurred in System.dll

Additional information: The system cannot find the file specified

这完全没有意义,因为我只是在分配一个变量。 我只想 运行 带有 snapshotOutput 变量的批处理文件。 这应该不难。

问题

  1. 知道如何获取批处理文件 运行 吗?和

  2. 当变量的设置甚至不应该查找文件时为什么会抛出错误?

  1. 您应该只明确传递批处理的路径和参数。否则 windows 尝试将参数合并到不存在的路径中。
  2. 这是 Visual Studio 中的视错觉,通常是由旧文件或代码优化引起的。

请看这里:

删除这些代码

string batFilename = batCommand + " " + snapshotOutput;

您必须更改进程启动

Process.Start(new ProcessStartInfo(batCommand , snapshotOutput))