0

我正在用 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 文件会根据文件名在不同的位置生成。意味着 .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 文件。简而言之,我在其中解压缩文件的文件夹,在我希望 .exe 写入 .csv 文件的同一文件夹中。例如,考虑有一个 D:/XYZ 文件夹,我在其中解压缩我的 zip 文件。在这个文件夹中有 test.grib 文件。我想为 test.grib 运行 exe 并生成 .csv 文件。我希望将这些 .csv 文件写入 XYZ 文件夹。

4

3 回答 3

1

processStartInfo.WorkingDirectory = @"你的目录";

请试试这个。

于 2012-10-09T07:17:01.703 回答
0

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

于 2012-10-09T07:01:20.357 回答
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 的文件路径。这是你追求的那种东西吗?

于 2012-10-09T07:06:04.010 回答