抛出了C#System.InvalidOperationException:无法启动进程,因为尚未提供文件名

时间:2016-06-17 09:06:57

标签: c#

命令行“XVirus.Start();”给了我“抛出System.InvalidOperationException:无法启动进程因为没有提供文件名。”。 但在此之前,它开始我的程序没有任何问题。代码在发生之前和之后仍然是相同的。如果您需要,这是我的代码。

using System;
using System.Diagnostics;
using System.IO;

namespace FDVirusX
{
    class MainClass
{
    public static void Main(string[] args)
    {
        var XVirus = new Process();

        var driveList = DriveInfo.GetDrives();

        foreach (DriveInfo drive in driveList)
        {
            if (drive.DriveType == DriveType.Removable)
            {
                Console.WriteLine("Scanning Flash Drive: " + drive.Name + drive.VolumeLabel);

                System.Threading.Thread.Sleep(1900);

                if (Directory.GetFiles(drive.ToString(), "*.lnk", SearchOption.AllDirectories).Length == 0)
                {
                    XVirus.StartInfo.WorkingDirectory = drive.ToString();
                    XVirus.StartInfo.FileName = "cmd.exe";
                    XVirus.StartInfo.Arguments = " /c title FDVirusX & cls & @echo off & echo There are no shortcut viruses in this Flash Drive.";
                    XVirus.StartInfo.UseShellExecute = false;
                    XVirus.StartInfo.RedirectStandardOutput = true;
                }
                else 
                {
                    XVirus.StartInfo.WorkingDirectory = drive.ToString();
                    XVirus.StartInfo.FileName = "cmd.exe";
                    XVirus.StartInfo.Arguments = "/c title FDVirusX & cls & attrib -s -h -r /s /d & echo Files restored. & del /f /s *.lnk & echo Shortcut files deleted.";
                    XVirus.StartInfo.UseShellExecute = false;
                    XVirus.StartInfo.RedirectStandardOutput = true;
                }
                if (!Directory.Exists(drive.ToString()))
                {
                    XVirus.StartInfo.WorkingDirectory = @"C:/";
                    XVirus.StartInfo.FileName = "cmd.exe";
                    XVirus.StartInfo.Arguments = "/c title FDVirusX & cls & @echo off & echo Flash Drive not found.";
                    XVirus.StartInfo.UseShellExecute = false;
                    XVirus.StartInfo.RedirectStandardOutput = true;
                }
            }
        }
        XVirus.Start(); // System.InvalidOperationException has been thrown: Cannot start process because a file name has not been provided.
        Console.WriteLine(XVirus.StandardOutput.ReadToEnd());
        System.Threading.Thread.Sleep(2000);
        Console.WriteLine("Closing Application...");
        System.Threading.Thread.Sleep(3000);
        XVirus.WaitForExit();
        XVirus.Close();
    }
}

}

3 个答案:

答案 0 :(得分:1)

此driveList为空:

var driveList = DriveInfo.GetDrives();

或者没有可移动的:

   if (drive.DriveType == DriveType.Removable)

一些简单的调试会确定问题。

答案 1 :(得分:1)

您需要检查是否有可以处理的可移动驱动器。 仅当GetDrives方法返回任何可移动驱动器时才初始化XVirus变量。

public static void Main(string[] args)
{
    // Be explicit and keep it initialized as null, so you could 
    // check it at the end of the loop if there are drives to process
    Process XVirus = null;

    // We are interested only in the removable drives. 
    // Linq is good choice here
    foreach (DriveInfo drive in DriveInfo.GetDrives()
                       .Where(x => x.DriveType == DriveType.Removable && 
                                   x.IsReady)
    {
         XVirus = new Process();

         ..... initialize the XVirus operating parameters ...

         // and remove this block because is not needed now 
         // if (!Directory.Exists(drive.ToString()))
         // {
         //     ...
         // }
    }

    // At the end if XVirus is still null we know that 
    // no removable drives  are available
    if(XVirus == null)
        MessageBox.Show("You don't have any removable drive ready")
    else
        ..... process the found drive ....
}

另外,如果你想处理多个驱动器,那么你应该在foreach循环中移动处理驱动器的代码

答案 2 :(得分:1)

好像我不需要这条线

                    if (!Directory.Exists(drive.ToString()))
                {
                    XVirus.StartInfo.WorkingDirectory = @"C:/";
                    XVirus.StartInfo.FileName = "cmd.exe";
                    XVirus.StartInfo.Arguments = "/c title FDVirusX & cls & @echo off & echo Flash Drive not found.";
                    XVirus.StartInfo.UseShellExecute = false;
                    XVirus.StartInfo.RedirectStandardOutput = true;
                }

完全没有。感谢大家帮我解决我的代码问题。