Console App trying to do a ChDir?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris

    #1

    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:
    [color=blue]
    >CD ToASubDir[/color]

    I am using the System.IO.Direc tory 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.SetCu rrentDirectory( path) which runs but only changes the apps
    logical current directory, not its physical current directory. I even tried
    Environment.Cur rentDirectory = path; to no avail.

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

    TIA
    Chris
  • Ignacio Machin \( .NET/ C# MVP \)

    #2
    Re: Console App trying to do a ChDir?

    Hi,

    what is what you want to do?

    The closest to cd XXX is Environment.Cur rentDirectory


    cheers,

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



    "Chris" <Chris@discussi ons.microsoft.c om> wrote in message
    news:EEB02BCA-B5CA-47B3-9C82-1FD5001FE039@mi crosoft.com...[color=blue]
    > 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:
    >[color=green]
    >>CD ToASubDir[/color]
    >
    > I am using the System.IO.Direc tory 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.SetCu rrentDirectory( path) which runs but only changes the apps
    > logical current directory, not its physical current directory. I even
    > tried
    > Environment.Cur rentDirectory = path; to no avail.
    >
    > Can anybody point me at the correct function that will do a ChDir (CD)
    >
    > TIA
    > Chris[/color]


    Comment

    • Chris

      #3
      Re: Console App trying to do a ChDir?

      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.Cur rentDirectory 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

      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: Console App trying to do a ChDir?

        Chris <Chris@discussi ons.microsoft.c om> wrote:[color=blue]
        > I am afraid I did that already and it did not change the directory for the
        > command window.[/color]

        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 - <skeet@pobox.co m>
        Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

        If replying to the group, please do not mail me too

        Comment

        • Chris

          #5
          Re: Console App trying to do a ChDir?

          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

          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: Console App trying to do a ChDir?

            Chris <Chris@discussi ons.microsoft.c om> wrote:[color=blue]
            > 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?[/color]

            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 - <skeet@pobox.co m>
            Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

            If replying to the group, please do not mail me too

            Comment

            • Willy Denoyette [MVP]

              #7
              Re: Console App trying to do a ChDir?


              "Chris" <Chris@discussi ons.microsoft.c om> wrote in message
              news:EEB02BCA-B5CA-47B3-9C82-1FD5001FE039@mi crosoft.com...[color=blue]
              > 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:
              >[color=green]
              >>CD ToASubDir[/color]
              >
              > I am using the System.IO.Direc tory 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.SetCu rrentDirectory( path) which runs but only changes the apps
              > logical current directory, not its physical current directory. I even
              > tried
              > Environment.Cur rentDirectory = path; to no avail.
              >
              > Can anybody point me at the correct function that will do a ChDir (CD)
              >
              > TIA
              > Chris[/color]

              Why not use ChDir from the Microsoft.Visua lBasic namespace (in
              MicrosoftVisual Basic.dll)?

              Willy.


              Comment

              • Willy Denoyette [MVP]

                #8
                Re: Console App trying to do a ChDir?


                "Chris" <Chris@discussi ons.microsoft.c om> wrote in message
                news:EC4763B5-2ABF-4774-9903-0D6F4CFC9E7B@mi crosoft.com...[color=blue]
                > 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.Cur rentDirectory 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[/color]

                Yep, Kernel32.dll - SetCurrentDirec tory

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

                Willy.



                Comment

                • Jon Skeet [C# MVP]

                  #9
                  Re: Console App trying to do a ChDir?

                  Willy Denoyette [MVP] <willy.denoyett e@telenet.be> wrote:[color=blue]
                  > Why not use ChDir from the Microsoft.Visua lBasic namespace (in
                  > MicrosoftVisual Basic.dll)?[/color]

                  That doesn't do what the OP wants - I believe it only does the
                  equivalent to Directory.SetCu rrentDirectory, 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 - <skeet@pobox.co m>
                  Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                  If replying to the group, please do not mail me too

                  Comment

                  • Willy Denoyette [MVP]

                    #10
                    Re: Console App trying to do a ChDir?


                    "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
                    news:MPG.1cfd8e 607bbaa66098c1a c@msnews.micros oft.com...[color=blue]
                    > Willy Denoyette [MVP] <willy.denoyett e@telenet.be> wrote:[color=green]
                    >> Why not use ChDir from the Microsoft.Visua lBasic namespace (in
                    >> MicrosoftVisual Basic.dll)?[/color]
                    >
                    > That doesn't do what the OP wants - I believe it only does the
                    > equivalent to Directory.SetCu rrentDirectory, 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 - <skeet@pobox.co m>
                    > http://www.pobox.com/~skeet
                    > If replying to the group, please do not mail me too[/color]

                    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.[color=blue]
                    >[/color]

                    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.




                    Comment

                    • Jon Skeet [C# MVP]

                      #11
                      Re: Console App trying to do a ChDir?

                      Jon Skeet [C# MVP] <skeet@pobox.co m> wrote:[color=blue][color=green]
                      > > However could you expand on the comment about *parent* process if it is
                      > > still relevant?[/color]
                      >
                      > 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.[/color]

                      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 - <skeet@pobox.co m>
                      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                      If replying to the group, please do not mail me too

                      Comment

                      • Chris

                        #12
                        RE: Console App trying to do a ChDir?

                        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 WriteProcesMemo ry() 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
                        CreateToolhelp3 2Snapshot 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

                        Comment

                        Working...