为什么exe不在程序文件中运行?

时间:2012-12-04 08:43:11

标签: c# exe system.diagnostics grib

我正在开发wpf应用程序。我正在使用以下代码运行degrib.exe

 public static void GenerateCsvFile(string fileName)
        {
            try
            {
                MessageBox.Show("Csv Error");
                MessageBox.Show("File Name : " + fileName);
                MessageBox.Show("WorkingDirectory" + new FileInfo(fileName).DirectoryName);

                System.Diagnostics.Process process = new System.Diagnostics.Process();
                System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
                startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
                startInfo.FileName = @"C:\ndfd\degrib\bin\degrib.exe";
                startInfo.Arguments = @"" + fileName + "" + " -C -msg 1 -Csv";
                startInfo.UseShellExecute = true;
                startInfo.WorkingDirectory = new FileInfo(fileName).DirectoryName;
                process.StartInfo = startInfo;
                process.Start();
                process.WaitForExit();
                process.Close();

                System.Diagnostics.Process process1 = new System.Diagnostics.Process();
                System.Diagnostics.ProcessStartInfo startInfo1 = new System.Diagnostics.ProcessStartInfo();
                startInfo1.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
                startInfo1.FileName = @"C:\ndfd\degrib\bin\degrib.exe";
                startInfo1.Arguments = @"" + fileName + "" + " -C -msg all -nMet -Csv";
                startInfo1.UseShellExecute = true;
                startInfo1.WorkingDirectory = new FileInfo(fileName).DirectoryName;
                process1.StartInfo = startInfo1;
                process1.Start();
                process1.WaitForExit();
                process1.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Degrib Error");
                Utility.ShowErrorMessage(ex);
            }
        }

degrib.exe从grib文件生成csv文件。这里http://www.nws.noaa.gov/mdl/degrib/index.php是对degrib的解释。在上面的函数中,fileName是grib文件的路径。如果grib文件放在Program Files以外的任何文件夹中,则上述函数会从grib文件中创建csv文件。如果我已将grib文件放在Program Files或Program Files中的任何其他文件夹中,则相同的函数将不会生成csv文件。相同的功能也不适用于AppData文件夹。为什么会这样?能不能就上述问题向我提供任何解决方案?

1 个答案:

答案 0 :(得分:0)

默认情况下,UACVista中的Win7不会在程序文件中为应用程序提供写入权限,除非它们是通过提升访问权限启动的。 你可以通过添加:

来做到这一点
startInfo.Verb = "runas";

然而,阅读degrib手册我注意到它接受输入和输出参数。 因此,在您的情况下,建议您为生成的csv文件使用预定义位置。 http://www.nws.noaa.gov/mdl/degrib/txtview.php?file=degrib.txt&dir=base

   -in [File]
     You can provide the file to read the GRIB message from either:
     1) right after "degrib",
     2) using the "-in [File]" option,
     3) on standard input.
   -out [File]
      Name of the file to store the results.  Must have 1 and only 1 dot in
      the name, and the extension must be 3 characters.  The extension will
      be replaced depending on file format.
   -namePath [Path]
      Name of a directory to put the files in.
      Only applicable if -out is NOT Specified.