简体   繁体   中英

Batch file with C# console app, how to stop the .bat file from excuting?

I have a batch file. I also have a C# CONSOLE APP that checks whether a certain set of files exist in a certain directory. what I need is: If the files are present then continue to run the next steps in the .bat file. Otherwise EXIT.

I need some help with how the console app can talk to the .bat file ( you know what I mean ) and then stop the rest of the .bat file from running if the input files were not present.

It's really unclear what you are asking for, however let me try to answer. I believe you want to continue the program is "some files" exist, otherwise you will exit the console app.

Assuming these files are somewhere which you can access consistently through a directory, and their path won't be changing, you could use the following code

string pathToFiles = @"C:/Users/YOUR_USER/"; //wherever

string[] fileNames = { "wordDocument.docx", "application.exe", "list.txt" }; //etc

static void Main()
{
    foreach (string file in fileNames)
    {
        if (File.Exists(Path.Combine(pathToFiles, file)))
        {
            continueTheProgram();
        }
        else
        {
            Environment.Exit(-1); //-1 can signify that the file does not exist
        }
    }
}

Edit: I'm noticing that you mention a batch file. Why not just have the Console App do all the work, rather than bouncing between the two?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM