I like to trim the following string:
"c:\program file\MTC\MTC\BIN\DEBUG"
into
"c:\program file\MTC"
What is the best way to do this? Thanks!
Printable View
I like to trim the following string:
"c:\program file\MTC\MTC\BIN\DEBUG"
into
"c:\program file\MTC"
What is the best way to do this? Thanks!
Use the IO.Path class to manipulate file and folder paths. IO.Path.GetDirectoryName will trim from the last slash from a path. Call GetDirectoryName three times on your path and you'll get the desired result. Of course, how exactly you would know to call it three times is up to you. How do you know now that you want to remove the lowest three folders?
This might also work but wouldn't be anywhere as robust as the previously suggested method. This is more my level: :rolleyes:
Code:Dim str As String = "c:\program file\MTC\MTC\BIN\DEBUG"
Dim cnt, i As Integer
For Each chr As Char In str
i += 1
If chr = "\" Then cnt += 1
If cnt = 3 Then
str = str.Substring(0, i - 1)
Exit For
End If
Next
MessageBox.Show(str)
Thank you both.
Well the story behide this question is that I need to find out what directory user installed my app to. In my app I need to referrence some sub-directories under the application folder. The only way I know how is to use Application.StartupPath. But Application.StartupPath returns {Application folder}\{Product Name}\bin\debug. In the above case, it's c:\Program File\MTC\MTC\bin\debug. Since I am interested in only c:\Program File\MTC, so I need to trim the rest of the string.
Is there any easier way to get the application folder, so I don't need to go through the trimming? If there is, can you please let me know? Thanks!
Application.StartupPath returns the path to the folder containing the current executable. It will obvioulsy return the bin\Debug path while debugging because that's where the executable is located. You shouldn't have a bin\Debug folder when you're running it on a machine on which it's been installed though. If you do then you've messed up your installer.
It is giving you that long path (\bin\debug) because that is where the .exe is created when you are developing the program. After you are done, assuming you have some sort of installation package or whatever, the pathname will not have those things. So, if chopping off the last three folders works now, it probably won't work later.
What is you application is installed in C:\ProgramFiles\MTC? This will no longer work because there aren't even three folders to chop.
That is what jmc was getting at with that statement.Quote:
Of course, how exactly you would know to call it three times is up to you. How do you know now that you want to remove the lowest three folders?
They way you are trying to solve this problem will only work for the moment. You need to think of a better way to get to the folder you want.
So what you guys are saying is that when I do a release build, and if user select to install the application in c:\Program File\MTC, Application.StartupPath will return c:\Program File\MTC, and NOT c:\Program File\MTC\MTC\bin\release? I am sorry, I am a little confused.
Exactly, or maybe MTC\MTC, I am not certain. The point is, it will change. Its called Application.StartupPath. So, it changes when the application gets moved.
Do, this. Create a program, add a button. Have the button do this
That will just put Application.StartupPath in a message box. Now, run it and you should have some sort of \bin\debug in it. Now go into bin\debug (in windows explorer, assuming you are using windows) and copy the .exe to your desktop and run it. You should see a difference. It should change depending on where you are running the exe.Code:msgbox(Application.StartupPath)
Oh, OK, I tried what you suggested. I have a better understanding now. Thank you very much!