Unable to set Process.StartInfo.WorkingDirectory to call exe from c#



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















I'm trying to call chrome.exe inside a C# program by using System.Diagnostics.Process namespace.



my chrome.exe is located inside path C:Program Files (x86)GoogleChromeApplication



if I call RunProc function by passing bellow parameters - (keep absolute path of the exe and keep WorkingDirectory empty)



("C:Program Files (x86)GoogleChromeApplicationChrome.exe" , "https://www.google.com", "") it works just fine.



But, with parameters -



("Chrome.exe , "https://www.google.com", "C:Program Files (x86)GoogleChromeApplication") it gives exception at step proc.Start(); stating - The system cannot find the file specified.



I also tried writing WorkingDirectory = workingDir while initializing StartInfo but still looking for solutions.



class Program

static void Main(string args)

RunProc(@"chrome.exe", @"https://www.google.com", @"C:Program Files (x86)GoogleChromeApplication");


static bool RunProc(string exe, string args, string workingDir)

Process proc = new Process

StartInfo =

FileName = exe,
CreateNoWindow = true,
RedirectStandardInput = true,
WindowStyle = ProcessWindowStyle.Hidden,
UseShellExecute = false,
RedirectStandardError = true,
RedirectStandardOutput = true,
Arguments = args,
//WorkingDirectory = workingDir

;

if (!string.IsNullOrEmpty(workingDir))

proc.StartInfo.WorkingDirectory = workingDir;


proc.Start();
proc.StandardInput.WriteLine(args);
proc.StandardInput.Flush();
proc.StandardInput.Close();

return true;












share|improve this question
























  • It would be awesome if you could provide a Minimal, Complete, and Verifiable example, so we can see in code how you are calling that method (and with what parameters).

    – mjwills
    Nov 15 '18 at 11:08











  • With this line: FileName = exe, does exe contain the full path to the file you want to start, or simply the file name? The Working Directory isn't used to execute the file

    – Martin
    Nov 15 '18 at 11:08











  • The Variable exe contains only file name. I want to set the working directory where the file is present. And if the file is available in your working directly so you should be able to run exe only with name.

    – Arkil Shaikh
    Nov 15 '18 at 13:41












  • @mjwills - I have updated the question, given full working code and an example.Hope this will help.

    – Arkil Shaikh
    Nov 16 '18 at 4:42


















1















I'm trying to call chrome.exe inside a C# program by using System.Diagnostics.Process namespace.



my chrome.exe is located inside path C:Program Files (x86)GoogleChromeApplication



if I call RunProc function by passing bellow parameters - (keep absolute path of the exe and keep WorkingDirectory empty)



("C:Program Files (x86)GoogleChromeApplicationChrome.exe" , "https://www.google.com", "") it works just fine.



But, with parameters -



("Chrome.exe , "https://www.google.com", "C:Program Files (x86)GoogleChromeApplication") it gives exception at step proc.Start(); stating - The system cannot find the file specified.



I also tried writing WorkingDirectory = workingDir while initializing StartInfo but still looking for solutions.



class Program

static void Main(string args)

RunProc(@"chrome.exe", @"https://www.google.com", @"C:Program Files (x86)GoogleChromeApplication");


static bool RunProc(string exe, string args, string workingDir)

Process proc = new Process

StartInfo =

FileName = exe,
CreateNoWindow = true,
RedirectStandardInput = true,
WindowStyle = ProcessWindowStyle.Hidden,
UseShellExecute = false,
RedirectStandardError = true,
RedirectStandardOutput = true,
Arguments = args,
//WorkingDirectory = workingDir

;

if (!string.IsNullOrEmpty(workingDir))

proc.StartInfo.WorkingDirectory = workingDir;


proc.Start();
proc.StandardInput.WriteLine(args);
proc.StandardInput.Flush();
proc.StandardInput.Close();

return true;












share|improve this question
























  • It would be awesome if you could provide a Minimal, Complete, and Verifiable example, so we can see in code how you are calling that method (and with what parameters).

    – mjwills
    Nov 15 '18 at 11:08











  • With this line: FileName = exe, does exe contain the full path to the file you want to start, or simply the file name? The Working Directory isn't used to execute the file

    – Martin
    Nov 15 '18 at 11:08











  • The Variable exe contains only file name. I want to set the working directory where the file is present. And if the file is available in your working directly so you should be able to run exe only with name.

    – Arkil Shaikh
    Nov 15 '18 at 13:41












  • @mjwills - I have updated the question, given full working code and an example.Hope this will help.

    – Arkil Shaikh
    Nov 16 '18 at 4:42














1












1








1








I'm trying to call chrome.exe inside a C# program by using System.Diagnostics.Process namespace.



my chrome.exe is located inside path C:Program Files (x86)GoogleChromeApplication



if I call RunProc function by passing bellow parameters - (keep absolute path of the exe and keep WorkingDirectory empty)



("C:Program Files (x86)GoogleChromeApplicationChrome.exe" , "https://www.google.com", "") it works just fine.



But, with parameters -



("Chrome.exe , "https://www.google.com", "C:Program Files (x86)GoogleChromeApplication") it gives exception at step proc.Start(); stating - The system cannot find the file specified.



I also tried writing WorkingDirectory = workingDir while initializing StartInfo but still looking for solutions.



class Program

static void Main(string args)

RunProc(@"chrome.exe", @"https://www.google.com", @"C:Program Files (x86)GoogleChromeApplication");


static bool RunProc(string exe, string args, string workingDir)

Process proc = new Process

StartInfo =

FileName = exe,
CreateNoWindow = true,
RedirectStandardInput = true,
WindowStyle = ProcessWindowStyle.Hidden,
UseShellExecute = false,
RedirectStandardError = true,
RedirectStandardOutput = true,
Arguments = args,
//WorkingDirectory = workingDir

;

if (!string.IsNullOrEmpty(workingDir))

proc.StartInfo.WorkingDirectory = workingDir;


proc.Start();
proc.StandardInput.WriteLine(args);
proc.StandardInput.Flush();
proc.StandardInput.Close();

return true;












share|improve this question
















I'm trying to call chrome.exe inside a C# program by using System.Diagnostics.Process namespace.



my chrome.exe is located inside path C:Program Files (x86)GoogleChromeApplication



if I call RunProc function by passing bellow parameters - (keep absolute path of the exe and keep WorkingDirectory empty)



("C:Program Files (x86)GoogleChromeApplicationChrome.exe" , "https://www.google.com", "") it works just fine.



But, with parameters -



("Chrome.exe , "https://www.google.com", "C:Program Files (x86)GoogleChromeApplication") it gives exception at step proc.Start(); stating - The system cannot find the file specified.



I also tried writing WorkingDirectory = workingDir while initializing StartInfo but still looking for solutions.



class Program

static void Main(string args)

RunProc(@"chrome.exe", @"https://www.google.com", @"C:Program Files (x86)GoogleChromeApplication");


static bool RunProc(string exe, string args, string workingDir)

Process proc = new Process

StartInfo =

FileName = exe,
CreateNoWindow = true,
RedirectStandardInput = true,
WindowStyle = ProcessWindowStyle.Hidden,
UseShellExecute = false,
RedirectStandardError = true,
RedirectStandardOutput = true,
Arguments = args,
//WorkingDirectory = workingDir

;

if (!string.IsNullOrEmpty(workingDir))

proc.StartInfo.WorkingDirectory = workingDir;


proc.Start();
proc.StandardInput.WriteLine(args);
proc.StandardInput.Flush();
proc.StandardInput.Close();

return true;









c# process system.diagnostics






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 4:42







Arkil Shaikh

















asked Nov 15 '18 at 10:57









Arkil ShaikhArkil Shaikh

785




785












  • It would be awesome if you could provide a Minimal, Complete, and Verifiable example, so we can see in code how you are calling that method (and with what parameters).

    – mjwills
    Nov 15 '18 at 11:08











  • With this line: FileName = exe, does exe contain the full path to the file you want to start, or simply the file name? The Working Directory isn't used to execute the file

    – Martin
    Nov 15 '18 at 11:08











  • The Variable exe contains only file name. I want to set the working directory where the file is present. And if the file is available in your working directly so you should be able to run exe only with name.

    – Arkil Shaikh
    Nov 15 '18 at 13:41












  • @mjwills - I have updated the question, given full working code and an example.Hope this will help.

    – Arkil Shaikh
    Nov 16 '18 at 4:42


















  • It would be awesome if you could provide a Minimal, Complete, and Verifiable example, so we can see in code how you are calling that method (and with what parameters).

    – mjwills
    Nov 15 '18 at 11:08











  • With this line: FileName = exe, does exe contain the full path to the file you want to start, or simply the file name? The Working Directory isn't used to execute the file

    – Martin
    Nov 15 '18 at 11:08











  • The Variable exe contains only file name. I want to set the working directory where the file is present. And if the file is available in your working directly so you should be able to run exe only with name.

    – Arkil Shaikh
    Nov 15 '18 at 13:41












  • @mjwills - I have updated the question, given full working code and an example.Hope this will help.

    – Arkil Shaikh
    Nov 16 '18 at 4:42

















It would be awesome if you could provide a Minimal, Complete, and Verifiable example, so we can see in code how you are calling that method (and with what parameters).

– mjwills
Nov 15 '18 at 11:08





It would be awesome if you could provide a Minimal, Complete, and Verifiable example, so we can see in code how you are calling that method (and with what parameters).

– mjwills
Nov 15 '18 at 11:08













With this line: FileName = exe, does exe contain the full path to the file you want to start, or simply the file name? The Working Directory isn't used to execute the file

– Martin
Nov 15 '18 at 11:08





With this line: FileName = exe, does exe contain the full path to the file you want to start, or simply the file name? The Working Directory isn't used to execute the file

– Martin
Nov 15 '18 at 11:08













The Variable exe contains only file name. I want to set the working directory where the file is present. And if the file is available in your working directly so you should be able to run exe only with name.

– Arkil Shaikh
Nov 15 '18 at 13:41






The Variable exe contains only file name. I want to set the working directory where the file is present. And if the file is available in your working directly so you should be able to run exe only with name.

– Arkil Shaikh
Nov 15 '18 at 13:41














@mjwills - I have updated the question, given full working code and an example.Hope this will help.

– Arkil Shaikh
Nov 16 '18 at 4:42






@mjwills - I have updated the question, given full working code and an example.Hope this will help.

– Arkil Shaikh
Nov 16 '18 at 4:42













2 Answers
2






active

oldest

votes


















1














The only way for this to work is for you to change your working directory to the passed in working directory before attempting to start the other process. The WorkingDirectory property is just that, and doesn't in any way get involved in locating the executable to run. That just relies on your working directory and your PATH environment variable, if you fail to provide a fully-qualified name.



static bool RunProc(string exe, string args, string workingDir)

var prevWorking = Environment.CurrentDirectory;
try

Environment.CurrentDirectory = workingDir;
Process proc = new Process

StartInfo =

FileName = exe,
CreateNoWindow = true,
RedirectStandardInput = true,
WindowStyle = ProcessWindowStyle.Hidden,
UseShellExecute = false,
RedirectStandardError = true,
RedirectStandardOutput = true,
Arguments = args,

;

proc.Start();
proc.StandardInput.WriteLine(args);
proc.StandardInput.Flush();
proc.StandardInput.Close();

return true;

finally

Environment.CurrentDirectory = prevWorking;







share|improve this answer

























  • That works well.. I interpreted workingDirectory property wrong, Thank you so much for clarifying that. This has solved my problem.

    – Arkil Shaikh
    Nov 16 '18 at 14:16











  • if you can edit line 6 like Environment.CurrentDirectory = workingDir; would be better for other viewers.

    – Arkil Shaikh
    Nov 16 '18 at 14:19











  • @ArkilShaikh - done. Glad you got it even with the typo.

    – Damien_The_Unbeliever
    Nov 16 '18 at 14:20


















0














Why not just call the .exe from the path where it is located directly ?



Process.Start(@"C:newfolderabcd.exe");


Or just put



proc.StartInfo.WorkingDirectory = @"c:newfolder";


before proc.start();






share|improve this answer























  • Thanks for the response. I can follow your first suggestion but the place I'm using this piece of code will have complex exe names, folder path and arguments, so it would be better to keep all these separate. I already tried your 2nd suggestion, that didn't work.

    – Arkil Shaikh
    Nov 15 '18 at 12:29











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53317899%2funable-to-set-process-startinfo-workingdirectory-to-call-exe-from-c-sharp%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














The only way for this to work is for you to change your working directory to the passed in working directory before attempting to start the other process. The WorkingDirectory property is just that, and doesn't in any way get involved in locating the executable to run. That just relies on your working directory and your PATH environment variable, if you fail to provide a fully-qualified name.



static bool RunProc(string exe, string args, string workingDir)

var prevWorking = Environment.CurrentDirectory;
try

Environment.CurrentDirectory = workingDir;
Process proc = new Process

StartInfo =

FileName = exe,
CreateNoWindow = true,
RedirectStandardInput = true,
WindowStyle = ProcessWindowStyle.Hidden,
UseShellExecute = false,
RedirectStandardError = true,
RedirectStandardOutput = true,
Arguments = args,

;

proc.Start();
proc.StandardInput.WriteLine(args);
proc.StandardInput.Flush();
proc.StandardInput.Close();

return true;

finally

Environment.CurrentDirectory = prevWorking;







share|improve this answer

























  • That works well.. I interpreted workingDirectory property wrong, Thank you so much for clarifying that. This has solved my problem.

    – Arkil Shaikh
    Nov 16 '18 at 14:16











  • if you can edit line 6 like Environment.CurrentDirectory = workingDir; would be better for other viewers.

    – Arkil Shaikh
    Nov 16 '18 at 14:19











  • @ArkilShaikh - done. Glad you got it even with the typo.

    – Damien_The_Unbeliever
    Nov 16 '18 at 14:20















1














The only way for this to work is for you to change your working directory to the passed in working directory before attempting to start the other process. The WorkingDirectory property is just that, and doesn't in any way get involved in locating the executable to run. That just relies on your working directory and your PATH environment variable, if you fail to provide a fully-qualified name.



static bool RunProc(string exe, string args, string workingDir)

var prevWorking = Environment.CurrentDirectory;
try

Environment.CurrentDirectory = workingDir;
Process proc = new Process

StartInfo =

FileName = exe,
CreateNoWindow = true,
RedirectStandardInput = true,
WindowStyle = ProcessWindowStyle.Hidden,
UseShellExecute = false,
RedirectStandardError = true,
RedirectStandardOutput = true,
Arguments = args,

;

proc.Start();
proc.StandardInput.WriteLine(args);
proc.StandardInput.Flush();
proc.StandardInput.Close();

return true;

finally

Environment.CurrentDirectory = prevWorking;







share|improve this answer

























  • That works well.. I interpreted workingDirectory property wrong, Thank you so much for clarifying that. This has solved my problem.

    – Arkil Shaikh
    Nov 16 '18 at 14:16











  • if you can edit line 6 like Environment.CurrentDirectory = workingDir; would be better for other viewers.

    – Arkil Shaikh
    Nov 16 '18 at 14:19











  • @ArkilShaikh - done. Glad you got it even with the typo.

    – Damien_The_Unbeliever
    Nov 16 '18 at 14:20













1












1








1







The only way for this to work is for you to change your working directory to the passed in working directory before attempting to start the other process. The WorkingDirectory property is just that, and doesn't in any way get involved in locating the executable to run. That just relies on your working directory and your PATH environment variable, if you fail to provide a fully-qualified name.



static bool RunProc(string exe, string args, string workingDir)

var prevWorking = Environment.CurrentDirectory;
try

Environment.CurrentDirectory = workingDir;
Process proc = new Process

StartInfo =

FileName = exe,
CreateNoWindow = true,
RedirectStandardInput = true,
WindowStyle = ProcessWindowStyle.Hidden,
UseShellExecute = false,
RedirectStandardError = true,
RedirectStandardOutput = true,
Arguments = args,

;

proc.Start();
proc.StandardInput.WriteLine(args);
proc.StandardInput.Flush();
proc.StandardInput.Close();

return true;

finally

Environment.CurrentDirectory = prevWorking;







share|improve this answer















The only way for this to work is for you to change your working directory to the passed in working directory before attempting to start the other process. The WorkingDirectory property is just that, and doesn't in any way get involved in locating the executable to run. That just relies on your working directory and your PATH environment variable, if you fail to provide a fully-qualified name.



static bool RunProc(string exe, string args, string workingDir)

var prevWorking = Environment.CurrentDirectory;
try

Environment.CurrentDirectory = workingDir;
Process proc = new Process

StartInfo =

FileName = exe,
CreateNoWindow = true,
RedirectStandardInput = true,
WindowStyle = ProcessWindowStyle.Hidden,
UseShellExecute = false,
RedirectStandardError = true,
RedirectStandardOutput = true,
Arguments = args,

;

proc.Start();
proc.StandardInput.WriteLine(args);
proc.StandardInput.Flush();
proc.StandardInput.Close();

return true;

finally

Environment.CurrentDirectory = prevWorking;








share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 16 '18 at 14:19

























answered Nov 16 '18 at 7:35









Damien_The_UnbelieverDamien_The_Unbeliever

198k17256347




198k17256347












  • That works well.. I interpreted workingDirectory property wrong, Thank you so much for clarifying that. This has solved my problem.

    – Arkil Shaikh
    Nov 16 '18 at 14:16











  • if you can edit line 6 like Environment.CurrentDirectory = workingDir; would be better for other viewers.

    – Arkil Shaikh
    Nov 16 '18 at 14:19











  • @ArkilShaikh - done. Glad you got it even with the typo.

    – Damien_The_Unbeliever
    Nov 16 '18 at 14:20

















  • That works well.. I interpreted workingDirectory property wrong, Thank you so much for clarifying that. This has solved my problem.

    – Arkil Shaikh
    Nov 16 '18 at 14:16











  • if you can edit line 6 like Environment.CurrentDirectory = workingDir; would be better for other viewers.

    – Arkil Shaikh
    Nov 16 '18 at 14:19











  • @ArkilShaikh - done. Glad you got it even with the typo.

    – Damien_The_Unbeliever
    Nov 16 '18 at 14:20
















That works well.. I interpreted workingDirectory property wrong, Thank you so much for clarifying that. This has solved my problem.

– Arkil Shaikh
Nov 16 '18 at 14:16





That works well.. I interpreted workingDirectory property wrong, Thank you so much for clarifying that. This has solved my problem.

– Arkil Shaikh
Nov 16 '18 at 14:16













if you can edit line 6 like Environment.CurrentDirectory = workingDir; would be better for other viewers.

– Arkil Shaikh
Nov 16 '18 at 14:19





if you can edit line 6 like Environment.CurrentDirectory = workingDir; would be better for other viewers.

– Arkil Shaikh
Nov 16 '18 at 14:19













@ArkilShaikh - done. Glad you got it even with the typo.

– Damien_The_Unbeliever
Nov 16 '18 at 14:20





@ArkilShaikh - done. Glad you got it even with the typo.

– Damien_The_Unbeliever
Nov 16 '18 at 14:20













0














Why not just call the .exe from the path where it is located directly ?



Process.Start(@"C:newfolderabcd.exe");


Or just put



proc.StartInfo.WorkingDirectory = @"c:newfolder";


before proc.start();






share|improve this answer























  • Thanks for the response. I can follow your first suggestion but the place I'm using this piece of code will have complex exe names, folder path and arguments, so it would be better to keep all these separate. I already tried your 2nd suggestion, that didn't work.

    – Arkil Shaikh
    Nov 15 '18 at 12:29















0














Why not just call the .exe from the path where it is located directly ?



Process.Start(@"C:newfolderabcd.exe");


Or just put



proc.StartInfo.WorkingDirectory = @"c:newfolder";


before proc.start();






share|improve this answer























  • Thanks for the response. I can follow your first suggestion but the place I'm using this piece of code will have complex exe names, folder path and arguments, so it would be better to keep all these separate. I already tried your 2nd suggestion, that didn't work.

    – Arkil Shaikh
    Nov 15 '18 at 12:29













0












0








0







Why not just call the .exe from the path where it is located directly ?



Process.Start(@"C:newfolderabcd.exe");


Or just put



proc.StartInfo.WorkingDirectory = @"c:newfolder";


before proc.start();






share|improve this answer













Why not just call the .exe from the path where it is located directly ?



Process.Start(@"C:newfolderabcd.exe");


Or just put



proc.StartInfo.WorkingDirectory = @"c:newfolder";


before proc.start();







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 15 '18 at 11:29









John LinaerJohn Linaer

54




54












  • Thanks for the response. I can follow your first suggestion but the place I'm using this piece of code will have complex exe names, folder path and arguments, so it would be better to keep all these separate. I already tried your 2nd suggestion, that didn't work.

    – Arkil Shaikh
    Nov 15 '18 at 12:29

















  • Thanks for the response. I can follow your first suggestion but the place I'm using this piece of code will have complex exe names, folder path and arguments, so it would be better to keep all these separate. I already tried your 2nd suggestion, that didn't work.

    – Arkil Shaikh
    Nov 15 '18 at 12:29
















Thanks for the response. I can follow your first suggestion but the place I'm using this piece of code will have complex exe names, folder path and arguments, so it would be better to keep all these separate. I already tried your 2nd suggestion, that didn't work.

– Arkil Shaikh
Nov 15 '18 at 12:29





Thanks for the response. I can follow your first suggestion but the place I'm using this piece of code will have complex exe names, folder path and arguments, so it would be better to keep all these separate. I already tried your 2nd suggestion, that didn't work.

– Arkil Shaikh
Nov 15 '18 at 12:29

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53317899%2funable-to-set-process-startinfo-workingdirectory-to-call-exe-from-c-sharp%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo