1

I've had a google search and looked through Stackoverflow answers already and can't find anything similar so I thought I'd make a question myself.

using System;
using System.IO;
using System.Diagnostics;

namespace changesProgram3
{
    public class Program
    {
        //Check to see if changes are made to a text file
        static void Main()
        {
            Process p = new Process();
            // Redirect the output stream of the child process.
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.FileName = "testbat.bat";
            p.Start();
            // Do not wait for the child process to exit before
            // reading to the end of its redirected stream.
            // Read the output stream first and then wait.
            string output = p.StandardOutput.ReadToEnd();
            Console.WriteLine(output);

        }
    }
}

It breaks on the "p.Start();" line during runtime with the error message: "The system can not find the file specified". I can't for the life of me work out what is going wrong. The testbat.bat file is in the same directory as the .csproj so perhaps it can't find that for some reason. Any help would be appreciated. Thanks.

normyp
  • 184
  • 1
  • 11
  • 2
    The file needs to be in the same folder as the executable (or the current working directory) – UnholySheep Aug 27 '17 at 12:39
  • Sorry I didn't fully understand that. The file is in with the .csproj there is no executable file. – normyp Aug 27 '17 at 12:45
  • See here: https://stackoverflow.com/questions/97312/how-do-i-find-out-what-directory-my-console-app-is-running-in-with-c This will show you where your working directory is. – Dennis Kuypers Aug 27 '17 at 12:46
  • 1
    To add to the good advice you are already given, the .csproj file is not the executable. When you compile the project you get a .exe file, usually in the `bin` folder (but you can change the Output path). Within the `bin` folder you have a folder for each build configuration - Debug, Release, etc. – Crowcoder Aug 27 '17 at 12:58
  • I found the solution to the problem. I had to put the full path name in where the .StartInfo.FileName and I had to bizarrely use forward slashes instead of backslashes. I also had to cd into the working directory in the batch file to comp the text files in there. – normyp Aug 27 '17 at 21:41

1 Answers1

1

try to give the full file path... p.StartInfo.FileName = @"c:\...\testbat.bat";

or

make it same folder if it's in the project make the file copy to debug\release folder after rebuilding the project

Leon Barkan
  • 2,676
  • 2
  • 19
  • 43