如何通过运行.exe在特定文件夹中生成输出

时间:2012-10-09 06:58:58

标签: c# wpf exe system.diagnostics

我正在用C#开发wpf应用程序。我能够运行以下exe文件

public static void GenerateCsvFile(string fileName)
        {
            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";
            process.StartInfo = startInfo;
            process.Start();

            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";
            process1.StartInfo = startInfo1;
            process1.Start();
        }

上面的代码为我成功生成了csv文件。但.csv文件是根据fileName在不同位置生成的。意味着.csv文件每次都在不同的文件夹中生成。我可以强制exe在特定文件夹中生成.csv文件吗?能否请您提供我可以解决上述问题的任何代码或链接?

编辑:用户选择zip文件并对表单进行汇总。 App.ApplicationPath是硬编码路径。以下是我的代码

private void ShowPointsButton_Click(object sender, RoutedEventArgs e)
        {
            ZipHelper.UnZip(FileNameTextBox.Text, App.ApplicationPath, safeFileName, 9999999);
}




public static void UnZip(string SrcFile, string DstFile, string safeFileName, int bufferSize)
        {
            //ICSharpCode.SharpZipLib.Zip.UseZip64.Off;

            FileStream fileStreamIn = new FileStream(SrcFile, FileMode.Open, FileAccess.Read);

            ZipInputStream zipInStream = new ZipInputStream(fileStreamIn); ;
            //if (SrcFile.Contains(".bz2"))
            //{
            //BZip2InputStream zipInStream = new BZip2InputStream(fileStreamIn);
            //}
            //else
            //{
            //    zipInStream = new ZipInputStream(fileStreamIn);
            //}


            string rootDirectory = string.Empty;
            if (safeFileName.Contains(".zip"))
            {
                rootDirectory = safeFileName.Replace(".zip", string.Empty);
            }
            else
            {
                rootDirectory = safeFileName;
            }

            Directory.CreateDirectory(App.ApplicationPath + rootDirectory);

            while (true)
            {
                ZipEntry entry = zipInStream.GetNextEntry();

                if (entry == null)
                    break;

                if (entry.Name.Contains("/"))
                {
                    string[] folders = entry.Name.Split('/');

                    string lastElement = folders[folders.Length - 1];
                    var folderList = new List<string>(folders);
                    folderList.RemoveAt(folders.Length - 1);
                    folders = folderList.ToArray();

                    //string folderPath = "";
                    //foreach (string str in folders)
                    //{
                        //folderPath = folderPath + @"\" + str;
                        //string blackslash = folderPath.Substring(0, 1);

                        //if (blackslash == "\\")
                        //{
                        //    folderPath = folderPath.Remove(0, 1);
                        //}

                        //if (!Directory.Exists(App.ApplicationPath + rootDirectory + "/" + folderPath))
                        //{
                        //    Directory.CreateDirectory(App.ApplicationPath + rootDirectory + "/" + folderPath);
                        //}
                    //}

                    if (!string.IsNullOrEmpty(lastElement))
                    {
                        //folderPath = folderPath + @"\" + lastElement;

                        //string blackslash = folderPath.Substring(0, 1);

                        //if (blackslash == "\\")
                        //{
                        //    folderPath = folderPath.Remove(0, 1);
                        //}

                        WriteToFile(DstFile + rootDirectory + @"\" + lastElement, bufferSize, zipInStream, rootDirectory, entry);
                    }

                }
                else
                {
                    WriteToFile(DstFile + rootDirectory + @"\" + entry.Name, bufferSize, zipInStream, rootDirectory, entry);
                }
            }

            zipInStream.Close();
            fileStreamIn.Close();
        }

private static void WriteToFile(string DstFile, int bufferSize, ZipInputStream zipInStream, string rootDirectory, ZipEntry entry)
        {
            WriteFileContents(DstFile, bufferSize, zipInStream);

            if (DstFile.Contains(".grb"))
            {
                Utility.GenerateCsvFile(DstFile);
            }

            //if(DstFile.Contains(".csv"))
            //{
            //    WriteFileContents(@"D:\Documents" + rootDirectory + @"\" + entry.Name, bufferSize, zipInStream);
            //}
        }

        private static void WriteFileContents(string DstFile, int bufferSize, ZipInputStream zipInStream)
        {
            FileStream fileStreamOut = new FileStream(DstFile, FileMode.OpenOrCreate, FileAccess.Write);
            int size;
            byte[] buffer = new byte[bufferSize];

            do
            {
                size = zipInStream.Read(buffer, 0, buffer.Length);
                fileStreamOut.Write(buffer, 0, size);
            } while (size > 0);

            fileStreamOut.Close();
        }

在上面的代码中,参见Utility.GenerateCsvFile(DstFile);我想在位置'DstFile'生成.csv文件。简而言之,我解压缩文件的文件夹,在我想要.exev文件的同一文件夹中。例如,考虑有D:/ XYZ文件夹,我在其中解压缩我的zip文件。在此文件夹中有test.grib文件。我想为test.grib运行exe并生成.csv文件。我希望将这些.csv文件写入XYZ文件夹。

3 个答案:

答案 0 :(得分:1)

processStartInfo.WorkingDirectory = @“你的目录”;

请试试这个。

答案 1 :(得分:0)

文件名需要指向正确的目录和文件,例如c:\ somedir \ mycsvfile.csv

答案 2 :(得分:0)

string folder = "c:\temp";
string fileName = "c:\somedir\blah\file.csv";

string outputFilePath = Path.Combine(folder, new FileInfo(fileName).Name);

那应该为您提供c:\ temp \ file.csv的文件路径。这是你追求的那种东西吗?