Console App trying to do a ChDir?

G

Guest

Hi,

For info I am using VS 2005 beta 2

I have a Console App and I want it to do the equivalent of a Change
Directory (CD) command that you would do in normal command line mode as
follows:
CD ToASubDir

I am using the System.IO.Directory namespace to query where I am and look
for available subdirectories etc. This works fine but I cannot find anything
in that namespace that will physically change the directory. I have tried
Directory.SetCurrentDirectory(path) which runs but only changes the apps
logical current directory, not its physical current directory. I even tried
Environment.CurrentDirectory = path; to no avail.

Can anybody point me at the correct function that will do a ChDir (CD)

TIA
Chris
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

what is what you want to do?

The closest to cd XXX is Environment.CurrentDirectory


cheers,
 
G

Guest

Ignacio,

I am afraid I did that already and it did not change the directory for the
command window.
In VB there appears to be a ChDir command, that I think is what I want to
do, only in c# but there does not appear to be an equivalent.

I want the end result to be that the command window executes a ChDir (CD)
just as if I had done it by hand in the command window.
Environment.CurrentDirectory does not achieve that, as per my original post.

I am currently looking at calling something from kernel32 but I cant find
what the function is. There used to be a handy little utility that listed all
the api calls and the parameters they used that came with VB 3/4/5/6 ??? one
or all of the above. Is that still available somewhere?

Thanks anyway
Chris
 
J

Jon Skeet [C# MVP]

Chris said:
I am afraid I did that already and it did not change the directory for the
command window.

I don't believe you can do that. It sounds like don't want to change
the current directory for the process, but for the *parent* process,
which is something entirely different. (It's also pretty unfriendly,
IMO. If I've changed to one particular directory in a command window,
it's because I want to be there.)
 
G

Guest

Jon

Well thats what I want to do !!!

I am writing a little processor that I am calling CDL its function is to
list all the subdirectories or the current directory with an associated index
number. It then lets me keyin a number (rather that keying in a
10/20/30/40/50... char folder name that I will almost always misspell at
least once, and is then supposed to CD to the selected subfolder. Its
therefore very friendly IMO, and more to the point completely on spec.

Sorry about that, it sounded a bit like a rant. I am getting a little
frustrated with this now. It was supposed to take me an hour to code this as
I didnt think it was going to be rocket science.

However could you expand on the comment about *parent* process if it is
still relevant?

Chris
 
J

Jon Skeet [C# MVP]

Chris said:
Well thats what I want to do !!!

I am writing a little processor that I am calling CDL its function is to
list all the subdirectories or the current directory with an associated index
number. It then lets me keyin a number (rather that keying in a
10/20/30/40/50... char folder name that I will almost always misspell at
least once, and is then supposed to CD to the selected subfolder. Its
therefore very friendly IMO, and more to the point completely on spec.

Sorry about that, it sounded a bit like a rant. I am getting a little
frustrated with this now. It was supposed to take me an hour to code this as
I didnt think it was going to be rocket science.

However could you expand on the comment about *parent* process if it is
still relevant?

It's definitely relevant, but I'm not sure how to go about it.
Fortunately I know someone who definitely *does* know how to do this
(in native code at least) - I'll ask them and get back to you.
 
W

Willy Denoyette [MVP]

Chris said:
Hi,

For info I am using VS 2005 beta 2

I have a Console App and I want it to do the equivalent of a Change
Directory (CD) command that you would do in normal command line mode as
follows:


I am using the System.IO.Directory namespace to query where I am and look
for available subdirectories etc. This works fine but I cannot find
anything
in that namespace that will physically change the directory. I have tried
Directory.SetCurrentDirectory(path) which runs but only changes the apps
logical current directory, not its physical current directory. I even
tried
Environment.CurrentDirectory = path; to no avail.

Can anybody point me at the correct function that will do a ChDir (CD)

TIA
Chris

Why not use ChDir from the Microsoft.VisualBasic namespace (in
MicrosoftVisualBasic.dll)?

Willy.
 
W

Willy Denoyette [MVP]

Chris said:
Ignacio,

I am afraid I did that already and it did not change the directory for the
command window.
In VB there appears to be a ChDir command, that I think is what I want to
do, only in c# but there does not appear to be an equivalent.

I want the end result to be that the command window executes a ChDir (CD)
just as if I had done it by hand in the command window.
Environment.CurrentDirectory does not achieve that, as per my original
post.

I am currently looking at calling something from kernel32 but I cant find
what the function is. There used to be a handy little utility that listed
all
the api calls and the parameters they used that came with VB 3/4/5/6 ???
one
or all of the above. Is that still available somewhere?

Thanks anyway
Chris

Yep, Kernel32.dll - SetCurrentDirectory

is what you are looking for, but better use Microsoft.VisualBasic namespace
stuff for this (see my other reply).

Willy.
 
J

Jon Skeet [C# MVP]

Willy Denoyette said:
Why not use ChDir from the Microsoft.VisualBasic namespace (in
MicrosoftVisualBasic.dll)?

That doesn't do what the OP wants - I believe it only does the
equivalent to Directory.SetCurrentDirectory, which includes the
following in the docs:

<quote>
When the application terminates, the working directory is restored to
its original location (the directory where the process was started).
</quote>

The OP wants something where from a console prompt in one directory he
can run his app, and after it completes the console prompt will be in
another directory.
 
W

Willy Denoyette [MVP]

Jon Skeet said:
That doesn't do what the OP wants - I believe it only does the
equivalent to Directory.SetCurrentDirectory, which includes the
following in the docs:

<quote>
When the application terminates, the working directory is restored to
its original location (the directory where the process was started).
</quote>

The OP wants something where from a console prompt in one directory he
can run his app, and after it completes the console prompt will be in
another directory.

Right, I focused too much on ...
<
In VB there appears to be a ChDir command, that I think is what I want to
do, only in c# but there does not appear to be an equivalent.
without paying attention on OP's exact requirements.
Anyway, there is no way (well, not that I know) to change the CWD of a
parent's process, you can change the current process CWD and that of your
children.

Willy.
 
J

Jon Skeet [C# MVP]

Jon Skeet said:
It's definitely relevant, but I'm not sure how to go about it.
Fortunately I know someone who definitely *does* know how to do this
(in native code at least) - I'll ask them and get back to you.

Unfortunately, I've just checked with them, and it was a 16 bit DOS
application they were using before - as far as they know, it can't be
done under Win32. Basically, you can't change the environment of a
parent process, including the working directory - at least as far as
they know.
 
G

Guest

Jon / Willy / anybody,

Ok I am beginning to believe you when you say "it cant be done".

I write this just in case it jogs your ( or anybody elses ) memory about how
I could achieve what I am trying to do, as I am only one step from a result.
(One small step for mankind, one giant step for me)

I have managed to get quite familiar with the Process component and calling
Win32 functions and using unsafe clr code to achieve all sorts of peeking at
processes running on my system. So much so I am probably half way to
re-writting the taskmanager ( not very productive ). But I cant see a way of
poking a CWD back into my calling process.
I did look at WriteProcesMemory() but I cannot see how to find where the CWD
is stored, and even if I did would it notice the change. Also the possibility
for total anarchy prevailing when getting address's wrong caused me to shy
away from that approach. (Coward I hear you cry)

I can find my process and therefore find the ProcessID of the ParentProcess
that launched me by using
CreateToolhelp32Snapshot and Process32First / Process32Next and looking in
the PROCESS32ENTRY structure, but there ( for I guess obvious security
reason) does not seem to be anything documented about changing the calling
process from a child process.
I suppose I could start a new cmd.exe process with the new CWD setup but
that could get a bit window crazy so I wont go any further along that line.

So it occured to me ????
NEW QUESTION:
Is it possible to get a .NET console application to run in the process of
the command window that you use to launch your app. If it is then I think my
logic may just work. ( I think this is called lateral thinking )

Any ideas on this one?

Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top