|
-
Feb 27th, 2007, 07:04 PM
#1
Thread Starter
Addicted Member
Trim a string
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!
-
Feb 27th, 2007, 07:15 PM
#2
Re: Trim a string
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?
-
Feb 27th, 2007, 08:05 PM
#3
Re: Trim a string
This might also work but wouldn't be anywhere as robust as the previously suggested method. This is more my level:
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)
-
Feb 28th, 2007, 03:26 PM
#4
Thread Starter
Addicted Member
Re: Trim a string
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!
-
Feb 28th, 2007, 03:53 PM
#5
Re: Trim a string
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.
-
Feb 28th, 2007, 03:56 PM
#6
Fanatic Member
Re: Trim a string
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.
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?
That is what jmc was getting at with that statement.
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.
VB.Net 2008
.Net Framework 2.0
"Must you breathe? 'Cause I need heaven..."
-
Feb 28th, 2007, 04:16 PM
#7
Thread Starter
Addicted Member
Re: Trim a string
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.
-
Feb 28th, 2007, 04:21 PM
#8
Fanatic Member
Re: Trim a string
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
Code:
msgbox(Application.StartupPath)
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.
VB.Net 2008
.Net Framework 2.0
"Must you breathe? 'Cause I need heaven..."
-
Feb 28th, 2007, 04:36 PM
#9
Thread Starter
Addicted Member
Re: Trim a string
Oh, OK, I tried what you suggested. I have a better understanding now. Thank you very much!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|