Click here to Skip to main content
15,918,967 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have created one Python exe which is reading data from Config.Json. Now I want to call this exe from C# console application, but I am getting an error stating
HTML
<pre>Traceback (most recent call last):
  File "file.py", line 552, in <module>
  File "file.py", line 12, in read_JSON
FileNotFoundError: [Errno 2] No such file or directory: 'config.json'
[456] Failed to execute script filePathExe

I am trying to run through below code:
C#
string appArgs = @"D:\appFolder\config.json";
string appPath = @"D:\appFolder\filePathExe.exe";
Process proc = new Process();
ProcessStartInfo si = new ProcessStartInfo(appPath, appArgs);
si.WindowStyle = ProcessWindowStyle.Normal;
si.Verb = "runas";             // UAC elevation required.
si.UseShellExecute = true;     // Required for UAC elevation.
proc.StartInfo = si;
proc.Start();
proc.WaitForExit();

Can someone please help, how can I run this?

What I have tried:

I have attached code above which I am using.
Posted
Updated 5-Aug-21 9:25am
v2
Comments
Sandeep Mewara 30-Jul-20 6:32am    
You sure whereever you are running the program/exe, file is present at the expected location?
FileNotFoundError: [Errno 2] No such file or directory: 'config.json'
User-8621695 30-Jul-20 6:34am    
yes, both files are at same location. Also exe is working fine when I am trying to run it individually but not working from C# console application.
Garth J Lancaster 30-Jul-20 6:54am    
That's a bit odd .. try adding Hide   Copy CodeHide   Copy Code
si.WorkingDirectory = @"D:\appFolder";
ie Hide   Copy CodeHide   Copy Code
si.WindowStyle = ProcessWindowStyle.Normal;si.WorkingDirectory = @"D:\appFolder";
si.Verb = "runas";             // UAC elevation required.


It would also be good to check the python code that made the exe to double-check it's using thr fully qualified path etc to the config file
ZurdoDev 30-Jul-20 8:42am    
Please post as solution.
Garth J Lancaster 30-Jul-20 8:54am    
done, OP has posted anyway

As suggested by ZurdoDev although the OP has posted my comment ....

That's a bit odd .. try adding
si.WorkingDirectory = @"D:\appFolder";
making the code

si.WindowStyle = ProcessWindowStyle.Normal;
si.WorkingDirectory = @"D:\appFolder";
si.Verb = "runas";             // UAC elevation required.
 
Share this answer
 
As @Garth suggested above to add working directory,I added that and it worked for me. so code is:
C#
string appArgs = @"D:\appFolder\config.json";
string appPath = @"D:\appFolder\filePathExe.exe";
Process proc = new Process();
ProcessStartInfo si = new ProcessStartInfo(appPath, appArgs);
si.WindowStyle = ProcessWindowStyle.Normal;
si.WorkingDirectory = @"D:\appFolder";
si.Verb = "runas";             // UAC elevation required.
si.UseShellExecute = true;     // Required for UAC elevation.
proc.StartInfo = si;
proc.Start();
proc.WaitForExit();

Thanks,
 
Share this answer
 

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