Reputation: 3010
I think my question title is already crystal clear.
I am invoking Process.Start()
method by passing, "cmd.exe" as parameter. But somehow when I execute the program the command prompt that appeared has my .../bin/debug/
in my project folder as its directory. I wanted it to change to C:
instead.
Can someone advise me on this?
Upvotes: 3
Views: 12893
Reputation: 10221
This is the proper way to set a specified working directory for any kind of process:
var processStartInfo = new ProcessStartInfo();
processStartInfo.WorkingDirectory = @"c:\";
processStartInfo.FileName = "cmd.exe";
// set additional properties
Process proc = Process.Start(processStartInfo);
Upvotes: 14
Reputation: 5287
Edit:
Others have posted more eloquent solutions, a la Yuriy-Guts's...
Process.Start("cmd.exe", @"/k ""cd /d C:\""");
(How it works:
http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/cmd.mspx?mfr=true
... the '/k' is part of a post execution command, which keeps your cmd prompt open after the directory change.)
...if your only goal is to launch the command prompt, but I suggest wrapping them in error handling, e.g....
try
{
Process.Start("cmd.exe", @"/k ""cd /d C:\""");
}
catch(Exception e)
{
//Just in case...
Console.WriteLine(e.ToString());
string[] str=Directory.GetLogicalDrives();
Console.WriteLine( "Using C# Directory Class ,Available drives are:");
for(int i=0;i< str.Length;i++)
Console.WriteLine(str[i]);
//If fatal
//Environment.Exit(1)
}
Further, if you're doing other stuff in C:/ I believe the below solution is the most transparent.
Short Answer:
Your IDE is automatically dumping you in the debug directory, because that is the path it's programmed to place your executable. Your executable's point of reference to System objects is the folder it lives in. You'll have to use absolute indexing to get to the root location C:
you want to go to.
Long Answer with Code, Self Help Advice Try Google first, for basics: https://www.google.com/search?q=change+directory+c%23
First result: http://www.c-sharpcorner.com/UploadFile/chandrahundigam/WorkingWithDirectory07022005012852AM/WorkingWithDirectory.aspx
(It's poorly formatted, but contains good content.)
To paraphrase:
Add to your code:
using System;
using System.IO;
using System.MarshalByRefObject;
class DoStuff
{
char driveLetter;
...
void Initialize()
{
try
{
Directory.SetCurrentDirectory( string(driveLetter)+string(@":\");
}
catch(FileNotFoundException e)
{
//Just in case...
Console.WriteLine(e.ToString());
string[] str=Directory.GetLogicalDrives();
Console.WriteLine( "Using C# Directory Class ,Available drives are:");
for(int i=0;i< str.Length;i++)
Console.WriteLine(str[i]);
//If fatal
//Environment.Exit(1)
}
Process.Start("cmd.exe");
//Do whatever else you need to do in C:/ ...
}
Note I am new to C# and did not explicitly know how to do this, but it was relatively trivial to figure out. C# experts feel free to correct me if there's any flaws in my approach.
Upvotes: 2
Reputation: 2220
In addition to the solutions described here, cmd.exe
's arguments can accept a command that will be executed immediately after the command line is opened. Also, there's the /k
switch that will keep the command line running after executing the command. You can use these two things to achieve your goal:
Process.Start("cmd.exe", @"/k ""cd /d C:\""");
More info: Cmd parameters.
Upvotes: 2
Reputation: 602
var process = Process.Start(new ProcessStartInfo
{
WorkingDirectory = "C:\\",
FileName="cmd.exe"
});
Upvotes: 1