Click here to Skip to main content
15,886,788 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey so I'm brand new, in school, barely know anything. As practice I'm trying to make a little WPF app that lets me open hulu, netflix or crunchyroll via chrome. I'm wanting to use simple batch files to open the website, but I can't seem to get the batch file to run on button click. However I keep getting told that the batch file doesn't exist, despite it being right in the project folder. What assuredly obvious thing am I doing wrong?

What I have tried:

namespace And_Chill
{

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Process.Start(Netflix_run.bat);
        }

    }
Posted
Updated 3-Nov-21 18:35pm

1 solution

Try
C#
private void Button_Click(object sender, RoutedEventArgs e)
{
   string filename = "Netflix_run.bat";
   string parameters = $"/k \"{filename}\"";
   Process.Start("cmd", parameters);
}

Explanation:
In your code, you did not put Netflix_run.bat between double quotes. I'm surpised this would compile, unless you define a Netflix_run class having a string property named bat.
Moreover, .bat files are not executable files which can be launched directly. You have to use the command processor (cmd.exe) to execute a shell script.

The line string parameters = $"/k \"{filename}\""; is building the string /k "Netflix_run.bat"
 
Share this answer
 
v2
Comments
Paul Conn 4-Nov-21 0:43am    
Ok so I wrote this, wasn't sure what to put in place of parameters and so I put the filename variable, however all it does is open command prompt. Also, would you mind explaining what the fourth line you have there does?
string args = $"/k \"{filename}\"";
phil.o 4-Nov-21 0:51am    
Oups sorry that is an error from my part. Please see update solution.
The line you are referring to is just building pararameters variable, except I did not name it well...)
Paul Conn 4-Nov-21 1:00am    
Okay I get it! Thanks a bunch man, still didn't work, I got an error within CMD this time saying the Netflix_Run.bat couldn't be found, however I changed the variable filename to "start chrome Netflix.com and it simply did that through cmd, which totally works.
phil.o 4-Nov-21 1:17am    
This has something to do with the current working directory. The .bat file has to be in the same folder as the executable running that code. Or, you have to pass the full path to the .bat file. Remember to escape the backslashes if you are writing a path; or use the verbatim-string modifier.
// Escaped version
string filename = "C:\\Users\\Paul\\Documents\\Netflix_run.bat";

// Verbatim version
string filename = @"C:\Users\Paul\Documents\Netflix_run.bat";
BillWoodruff 4-Nov-21 9:17am    
+5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900