Вопрос

I'm writing a C# Windows program. On a form, I have a list of websites and PDFs. When they click on a PDF, the code downloads that file, then tries to open it. On my Windows 8 machine, the PDFs open fine. On Windows 7 and XP machines, they are not opening and there are no visual clues showing where things are breaking. (On both of these machines, you can click on a PDF in the file explorer and the PDFs will open.)

I started by looking at Opening a pdf in .NET, then added to that. Here's what I' using...

if (File.Exists(localFileName))
{
    //MessageBox.Show("calling file " + localFileName, "FYI...");
    System.Diagnostics.Process process = new System.Diagnostics.Process();
    System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
    startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
    startInfo.FileName = "cmd.exe";
    startInfo.Arguments = "call /c " + localFileName;
    process.StartInfo = startInfo;
    try
    {
        //MessageBox.Show("calling file " + localFileName, "FYI...");
        if (!process.Start())
        {
            MessageBox.Show("Could not invoke the PDF Viewer. Try opening the file at " + localFileName, 
                "Problem Opening File...", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            return;
        }
    }
    catch (Exception exc)
    {
        MessageBox.Show("Couldn't start process (call " + localFileName + "), Message=" + exc.Message, 
        "Problem Encounter...", MessageBoxButtons.OK, MessageBoxIcon.Error);
        return;
    }
    //MessageBox.Show("Should be done", "FYI...");
}

I am trying to detect whether the process starts or not. If I can't get them to execute, it would help if I could display a message telling the user where to look to open the downloaded file. Any thoughts?

Это было полезно?

Решение

Your code is totally wrong. There is absolutely no need for cmd.exe to be involved in this operation, nor is there a need for call /c (and there's absolutely no need to require elevation as your answer suggests).

if FileExists(localFileName) {
    try
    { 
      System.Diagnostics.Process.Start(localFileName); 
    catch
    }
      /* 
         Handle failure to start process (Win32Exception)
         No need to handle FileNotFoundException, because
         you can do that in the else below
      */
    };
}
else
{
  // Handle missing file
}

cmd.exe is not needed, as it opens a new instance of the Windows command processor (AKA a command prompt), and call /c is only needed when CALLing another batch file from within a batch file. There is certainly no need to elevate the process as your answer suggests.

Другие советы

The problem appears to be permission-related. I added process.Verb = "runas" to bump it up to admin level and it worked on the Windows 7 machine. On the XP machine, it prompted for an admin password, which isn't great but should be good enough for my purposes...

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top