C
C#15mo ago
Maybe Ange

❔ [FIX} Csharp & Python / WPF APP

I have a problem with this code, in fact I have a button on my WPF software and when I click on it it executes a separate python script in another file. The problem is that the software returns me my error I made in "Try" and tells me that it can't find the file and it can't run it, yet I don't understand the path is ok , can someone help me?
private void Button_Click(object sender, RoutedEventArgs e)
{
string pythonPath = @"C:\Program Files\Python\Python39\python.exe";
string scriptPath = @"D:\Documents\Script security network\Software Box\Software\Reload Box\scripts\reload-no-time.py";

try
{
if (!File.Exists(scriptPath))
{
throw new FileNotFoundException("Le fichier de script n'existe pas", scriptPath);
}
}
catch (FileNotFoundException ex)
{
MessageBox.Show($"Erreur : {ex.Message}");
return;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
string pythonPath = @"C:\Program Files\Python\Python39\python.exe";
string scriptPath = @"D:\Documents\Script security network\Software Box\Software\Reload Box\scripts\reload-no-time.py";

try
{
if (!File.Exists(scriptPath))
{
throw new FileNotFoundException("Le fichier de script n'existe pas", scriptPath);
}
}
catch (FileNotFoundException ex)
{
MessageBox.Show($"Erreur : {ex.Message}");
return;
}
33 Replies
mtreit
mtreit15mo ago
If it says the file does not exist it probably really does not exist. Are you sure the path is right? If you open a PowerShell window and type this what does it say?
dir "D:\Documents\Script security network\Software Box\Software\Reload Box\scripts\reload-no-time.py"
dir "D:\Documents\Script security network\Software Box\Software\Reload Box\scripts\reload-no-time.py"
Maybe Ange
Maybe Ange15mo ago
Maybe Ange
Maybe Ange15mo ago
mtreit
mtreit15mo ago
I don't see any reason that code would fail then
a coding witch
a coding witch15mo ago
why not just call "python"? it will work if you have the language properly installed in your OS since you have the path environment variable enabled
Maybe Ange
Maybe Ange15mo ago
Maybe Ange
Maybe Ange15mo ago
What?
mtreit
mtreit15mo ago
Your error message is supposed to say this: | Le fichier de script n'existe pas But the error message you just showed does not say that. So is that error message coming from somewhere else?
a coding witch
a coding witch15mo ago
yeah your program cant find python maybe? try only using "python" instead of the path just curious if itll work
Maybe Ange
Maybe Ange15mo ago
Just python, how?
a coding witch
a coding witch15mo ago
instead of passing the python path just type python [script path]
Maybe Ange
Maybe Ange15mo ago
I'm not very strong in Csharp lmao, how?
a coding witch
a coding witch15mo ago
are you instancing a new Process? to execute the script? can you show that part of the code?
Maybe Ange
Maybe Ange15mo ago
No
a coding witch
a coding witch15mo ago
put breakpoints in your errors your screenshot dont fit with the code you are showing
Maybe Ange
Maybe Ange15mo ago
a coding witch
a coding witch15mo ago
As I said, you are instancing a new Process can you show the entire code?
a coding witch
a coding witch15mo ago
Is your Python path really correct? I mean, your script path is correct
Maybe Ange
Maybe Ange15mo ago
Mhm
a coding witch
a coding witch15mo ago
but how about the Python exe ?
Maybe Ange
Maybe Ange15mo ago
Maybe Ange
Maybe Ange15mo ago
Oh script path?
a coding witch
a coding witch15mo ago
no you are using the wrong python path
Maybe Ange
Maybe Ange15mo ago
Oh
a coding witch
a coding witch15mo ago
check the screenshot you sent now and check your code
Maybe Ange
Maybe Ange15mo ago
Okay, Now I don't have the same code anymore depending on the 2 locations I put wait
Maybe Ange
Maybe Ange15mo ago
With: C:\Users\Cooky\AppData\Local\Programs\Python\Python311\python.exe
Maybe Ange
Maybe Ange15mo ago
With: C:\Users\Cooky\AppData\Local\Microsoft\WindowsApps\python.exe
Maybe Ange
Maybe Ange15mo ago
"Script exited with error code"
Maybe Ange
Maybe Ange15mo ago
Maybe Ange
Maybe Ange15mo ago
Mhmmm 🤔 My script works perfectly locally, why not from here?
Accord
Accord15mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.