-2

heads up, I am completely new to coding in c# and I'm still a student in my first year but I was given a project where I need to basically make a c# Console Application (no windows forms, just purely c#) where it opens a file or runs a program based on whatever the user inputs. In addition, add a file existence check which would either open the file/run exe if it exists or exit the program if it doesn't. I tried researching for help but didn't get anything specific but here is what I have so far and I'm stuck as for what I need to do next. Any help is appreciated as I am still learning

void RUNNER()
{
    // Capture user input
    Console.WriteLine("Enter directory for file/program to run");
    string userInput = Console.ReadLine();

    // Check for file existence
    if (File.Exists(userInput))
    {
        File.OpenRead(userInput);
        Console.WriteLine("This file exists, opening...");
    }
}

1 Answers1

0

Look at Process.Start and Process.StartInfo

Process process = Process.Start(userInput)

Note: If the file exists in bin directory, then you can call it directly, otherwise, you need to call in with path

Example:

Process process = Process.Start('sample.exe')

or

Process process = Process.Start(@'c:\dir\sample.exe')
Mironline
  • 2,755
  • 7
  • 35
  • 61
  • void RUNNER() { // Capture user input Console.WriteLine("Enter directory for file/program to run"); string userInput = Console.ReadLine(); // Check for file existence if (File.Exists(userInput)) { Console.WriteLine("This file exists, opening..."); Process.Start(userInput); } } I tried this, but the files I tried didn't open... did I say it incorrectly? – Moatiz Sabahat Oct 31 '22 at 16:50