Running console application from C # but application cannot create file

I have a Windows Forms application that launches another console application here is a piece of code

prog = new Process();
prog.StartInfo.FileName = exefile;

      

The console app should create a file, but when I run this app from C # it doesn't create any file when I run the console app with double click, it works fine here is a piece of code from "exefile" (its in C ++)

freopen("file.in","r",stdin);
freopen("file.out","w",stdout);
printf("somedata\n");

      

"file.in" probably exists

+1


a source to share


3 answers


Most likely you need to set a working path:

prog.StartInfo.WorkingDirectory = ...

      



i.e. I think it cannot find file.in in the current application folder.

+2


a source


You need to add this line when you want to start the process:

prog.Start();

      



Here is a link to the MSDN page for Process.Start

.
There are several overloads you can consider.

+1


a source


I would guess

  • handle exceptions to see what's going wrong.
  • as above make sure you call start () method

Here's a snippet of code from msdn that you might want to reference

Process myProcess = new Process();

        try
        {
            // Get the path that stores user documents.
            string myDocumentsPath = 
                Environment.GetFolderPath(Environment.SpecialFolder.Personal);

            myProcess.StartInfo.FileName = myDocumentsPath + "\\MyFile.doc"; 
            myProcess.StartInfo.Verb = "Print";
            myProcess.StartInfo.CreateNoWindow = true;
            myProcess.Start();
        }
        catch (Win32Exception e)
        {
            if(e.NativeErrorCode == ERROR_FILE_NOT_FOUND)
            {
                Console.WriteLine(e.Message + ". Check the path.");
            } 

            else if (e.NativeErrorCode == ERROR_ACCESS_DENIED)
            {
                // Note that if your word processor might generate exceptions
                // such as this, which are handled first.
                Console.WriteLine(e.Message + 
                    ". You do not have permission to print this file.");
            }
        }

      

0


a source







All Articles