0

I am developing wpf application in C#. I am able to run the following exe files

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();
        }

The above code generate the csv files for me successfully. But .csv files are generated at different location depending on the fileName. means .csv files are generated within different folder each time. Can I force the exe to generate the .csv file in specific folder ? Can you please provide me any code or link through which I can resolve the above issue ?

Edit: The user selects the zip file and sumbit the form. The App.ApplicationPath is hard coded path. The following is my code

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();
        }

In the above code see at line Utility.GenerateCsvFile(DstFile); I want to generate the .csv files at location 'DstFile'. In short, the folder in which I am unzipping my files, in the same folder I want .exe to write .csv file. For example consider there is D:/XYZ folder in which I am unzipping my zip files. In this folder there is test.grib file. I want to run exe for test.grib and produce .csv files. I want these .csv files to be written to XYZ folder.

animuson
  • 53,861
  • 28
  • 137
  • 147
Shailesh Jaiswal
  • 3,606
  • 13
  • 73
  • 124
  • Which `DstFile` do you mean? A `DstFile` parameter is passed into `UnZip`, at which time it will be `App.ApplicationPath`. This parameter is then modified before being passed into `WriteToFile`. Where exactly do things go wrong? – nick_w Oct 09 '12 at 20:05
  • @NickW. Everything is working fine for me. My problem is only with function Utility.GenerateCsvFile(DstFile); In the above code DstFile is path - D:\Projects\ApiRouting\ApiRouting\Files. And on this path I want my .csv to be generated after running the .exe. But it is generated in another folder – Shailesh Jaiswal Oct 10 '12 at 05:28
  • So is the problem that you want to pass a different value of `DstFile` to that method than what is currently being passed in? – nick_w Oct 10 '12 at 05:35
  • No. Suppose my grib file is located at location D:\Projects\ApiRouting\ApiRouting\Files\abc.grb then all the .csv files must be generated in D:\Projects\ApiRouting\ApiRouting\Files folder and not in any other folder after running exe – Shailesh Jaiswal Oct 10 '12 at 05:39
  • Currently it is generating the .csv files in D:/Documents – Shailesh Jaiswal Oct 10 '12 at 05:42
  • So if you are Degribbing `D:\Projects\ApiRouting\ApiRouting\Files\abc.grb`, then that would mean that `Utility.GenerateCsvFile` was called with this path. Is this correct? I presume that the directory you are unzipping to is the one created by `Directory.CreateDirectory(App.ApplicationPath + rootDirectory);` and that the degribbing is done with this call `WriteToFile(DstFile + rootDirectory + @"\" + entry.Name,...`. If those two things are true then I don't understand why this doesn't already work. – nick_w Oct 10 '12 at 06:11

3 Answers3

1

processStartInfo.WorkingDirectory = @"Your Directory";

Please try this.

Raghull
  • 26
  • 1
  • What is this supposed to do? Some context or description would be nice. – Emil Vikström Oct 09 '12 at 07:22
  • I think the problem with this approach is that the `fileName` parameter specifies an absolute path. The working directory wouldn't have much effect in that case. – nick_w Oct 09 '12 at 07:27
0

The file name needs to point to the correct directory and file, such as c:\somedir\mycsvfile.csv

animaonline
  • 3,715
  • 5
  • 30
  • 57
0
string folder = "c:\temp";
string fileName = "c:\somedir\blah\file.csv";

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

That should get you a file path of c:\temp\file.csv. Is this the kind of thing you were after?

nick_w
  • 14,758
  • 3
  • 51
  • 71
  • The above .exe runs and generate the .csv file in D:/Docs. Instead of D:/Docs I want to generate the .csv file in C:/xyz/test. How can I do this? – Shailesh Jaiswal Oct 09 '12 at 07:08
  • Where does the D:\Docs value come from? – nick_w Oct 09 '12 at 07:10
  • it is coming from startInfo1.FileName = @"C:\ndfd\degrib\bin\degrib.exe"; startInfo1.Arguments = @"D:\Documents\Pacificwind.grb -C -msg all -nMet -Csv"; – Shailesh Jaiswal Oct 09 '12 at 07:20
  • The D:/Docs means D:/Documents in my first comment – Shailesh Jaiswal Oct 09 '12 at 07:21
  • Well, you should be able to adapt my sample code for that. If you want complete control over the folder that the files are written to, consider passing an additional parameter into your `GenerateCsvFile` that specifies the folder. In that case your `startInfo1.Arguments` could be `Path.Combine(outputFolder, "Pacificwind.grb") + "-C -msg all -nMet -Csv"` – nick_w Oct 09 '12 at 07:25
  • Is there any more information you can provide? For example, what did you expect to happen and what actually happened. You can always edit your question with any changes you have made to your code. – nick_w Oct 09 '12 at 09:57
  • Hi @NickW. I have modified my question. Please see the question – Shailesh Jaiswal Oct 09 '12 at 11:42