Possible VB.NET bug??

  • Thread starter Thread starter Tygur
  • Start date Start date
T

Tygur

Guest
Consider this VB6 code:
Code:
Dim TestDir As String
TestDir = "d:\testdir"
ChDrive Left(TestDir, 1)
ChDir TestDir
ChDrive "c:"
MsgBox CurDir(Left(TestDir, 1))

Assuming that the TestDir string is a full path to a directory that exists, that directory should be shown in the messagebox. If you test it out, it will perform as expected.

Now consider this VB.NET code:
Code:
        Dim TestDir As String
        TestDir = "d:\testdir"
        ChDrive(Left(TestDir, 1))
        ChDir(TestDir)
        ChDrive("c:")
        MsgBox(CurDir(Left(TestDir, 1)))

This code should do the same exact thing as the VB6 code I just showed you. In fact, it was the Visual Studio .NET IDE that converted the code for me.

If you run that VB.NET code, the directory shown in the message box doesn't seem to be the same as the one in TestDir if the directory in TestDir is on any drive other than drive C (or whichever drive that last ChDrive line changes to). Did I just find a bug, or did I miss something?

NOTE: the culprit is this line (taken from the VB.NET code):
ChDrive("c:")

Apparently, it's changing the current path on the drive it's changing from in the process of changing the current drive.
 
Why would you use those old methods of doing this? Personally I've never used a "current directory" in any version of Visual Basic.

There is an Environment.CurrentDirectory, if that helps.
 
These "old methods" are still a part of the Visual Basic language. There is nothing in the help files that indicate that they should be avoided.

If I change this line:
ChDrive("c:")

..to this:
System.Environment.CurrentDirectory = "c:"

..or this:
System.IO.Directory.SetCurrentDirectory("c:")

...the results are still the same, by the way.

And yes, I know I could've left out the "System." part of both of those lines, but I left it in, anyway. (just in case anyone is thinking of commenting on that)
 
Back
Top