469,953 Members | 2,257 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,953 developers. It's quick & easy.

Console App trying to do a ChDir?

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
Nov 17 '05 #1
11 3062
Hi,

what is what you want to do?

The closest to cd XXX is Environment.CurrentDirectory
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Chris" <Ch***@discussions.microsoft.com> wrote in message
news:EE**********************************@microsof t.com...
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

Nov 17 '05 #2
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
Nov 17 '05 #3
Chris <Ch***@discussions.microsoft.com> wrote:
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.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #4
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
Nov 17 '05 #5
Chris <Ch***@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #6

"Chris" <Ch***@discussions.microsoft.com> wrote in message
news:EE**********************************@microsof t.com...
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


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

Willy.
Nov 17 '05 #7

"Chris" <Ch***@discussions.microsoft.com> wrote in message
news:EC**********************************@microsof t.com...
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.

Nov 17 '05 #8
Willy Denoyette [MVP] <wi*************@telenet.be> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #9

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Willy Denoyette [MVP] <wi*************@telenet.be> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


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.


Nov 17 '05 #10
Jon Skeet [C# MVP] <sk***@pobox.com> wrote:
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.


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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #11
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
Nov 17 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Andrew Crowe | last post: by
5 posts views Thread by Nicholas Geraldi | last post: by
4 posts views Thread by Yun Mao | last post: by
3 posts views Thread by dannycolligan | last post: by
reply views Thread by Julien Biezemans | last post: by
1 post views Thread by Giulio Petrucci | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.