VB.net Get portion of file path in a directory

Hi

I use the following code on a windows form to allow the user to select a file within a directory
called MY_DIR. I want to get the file path within that directory and convert this to a string.
So I don't want the whole file path. Just the portion that is in MY_DIR.


            Dim sFileAndPath As String = ""
            Dim sPath As String = ""
            Dim sFile As String = ""

            With OpenFileDialog1
                .FileName = ""
                .InitialDirectory = MY_DIR
                .ShowDialog()
                sFileAndPath = CStr(.FileName)
                sPath = CStr(Path.GetDirectoryName(sFileAndPath))
                sFile = CStr(Path.GetFileName(sFileAndPath))
            End With
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
Try something like this. I think it will give you what you need.
Dim partialPath As String = sFileAndPath.Remove(0, MY_DIR.Count())

Open in new window

0
Jacques Bourgeois (James Burger)PresidentCommented:
I would rather use

Dim partialPath As String = sFileAndPath.Remove(0, MY_DIR.Length)

Although Fernando's call works, in my experience most programmers won't understand what the Count is doing there. Programmers who come from VB6 are used to Length. Experienced .NET programmers are used to see Count as a property, not as a method. And most are not aware that under the hood, a String is treated as a collection of Char (the Count comes from that). Even the documentation for String.Count is not clear about what it will return. And Count is defined as an extension method, meaning that it is not part of the String class, while Lenght is.

You might also need to go Length+1 if MY_DIR does not already include the terminating \.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
it_saigeDeveloperCommented:
Another approach might be to use the String.Replace method; e.g. -
Imports System.IO

Module Module1
	ReadOnly MY_DIR As String = "C:\_admin\"
	Sub Main()
		Dim file As New FileInfo("C:\_admin\mydirectory\myfile.txt")
		If file.FullName.Contains(MY_DIR) Then
			Console.WriteLine("The user selected: {0}", file.FullName.Replace(MY_DIR, String.Empty))
		End If
		Console.ReadLine()
	End Sub
End Module

Open in new window

Which produces the following output -Capture.JPG-saige-
0
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Thanks very much
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.