473,385 Members | 1,355 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 3256
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Andrew Crowe | last post by:
Hi guys, We're using the ISAPI version of PHP with IIS5.0 On some websites for some reason any php file in the websites root is given the path winnt/system32 (getcwd() returns...
5
by: Nicholas Geraldi | last post by:
I have a script that uses the chdir() function. I know the script works ( works perfectly on my host ), but when I put the script on my server here at work I get the following error. Warning:...
4
by: Yun Mao | last post by:
Hi, How to make changes to os.environ and os.path.chdir still effective after I run the script? Currently, the changes are only good within my script. I would like the shell who called python...
1
by: Oz | last post by:
This is long. Bear with me, as I will really go through all the convoluted stuff that shows there is a problem with streams (at least when used to redirect stdout). The basic idea is that my...
3
by: dannycolligan | last post by:
I have a strange problem with os.chdir... here is my script that I am using to edit the filenames of my music library: #!/usr/bin/python from os import * ...
6
by: Ant | last post by:
Hi all, I'm putting together a simple help module for my applications, using html files stored in the application directory somewhere. Basically it sets up a basic web server, and then uses the...
16
by: matt | last post by:
I have used some free code for listing files for download, but I want to send an email to the administrator when the file has been downloaded. I have got some code in here that does it, but it will...
0
by: Julien Biezemans | last post by:
Hi everyone, I've written a file:// scheme stream wrapper that acts like chroot() (but is more flexible, does not require root privileges and works on any platform). Everything works just fine...
1
by: Giulio Petrucci | last post by:
Hi there, Hi have a console application which shoul "know" from where I'm running it. I thought it was a good idea to run a sort of "chdir" command from the application itself, but I don't know...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.