Mar 20, 2008
Programming Experience
I am writing a console application and I am trying to change directories. I am using Directory.SetCurrentDirectory() method and its changing directories just find. The problem is, however, when my application exits, it sets the current directory back to where I was. I also tried importing the SetCurrentDirectory API call from Kernel32.dll and using that instead of Directory.SetCurrentDirectory(). However, it does the same thing. It successfully changes the directory, however, it doesn't "stick" when the application terminates. Is there a way around this?

If there is no way to get around this, then my next question would be, is it possible to call old DOS interrupts from a console application? That way I could called Interrupt 0x21, function 3B, which DOES change the directory, even when the application terminates.

Any help would be appreciated!

I figured this out on my own. The solution is kinda "sloppy", but it works. In case anyone ever needs to do this, I will describe what I did to solve it. What I did was make a call the Windows API Function FindWindowA() which retreives me a handle to my console window. Then once I have the handle, I make a call to another Windows API function called PostMessage, and I "simulate" a keystroke to the console window by posting the WM_CHAR message. By simulating keystrokes, I simply do a 'CD "whatever directory"' and simulate a carriage return and presto, the directory changes. I then terminate the application and when my application terminates, it leaves me in the directory that I CDed to. If anyone needs any VB.NET examples, let me know.