Results 1 to 8 of 8

Thread: How to shorten the string of file path? [Resolved]

  1. #1

    Thread Starter
    Registered User
    Join Date
    Apr 2003
    Location
    Klang, Selangor, Malaysia
    Posts
    163

    Question How to shorten the string of file path? [Resolved]

    I add an OpenFileDialog to my project.
    In order to identify the path of the file that I want to open, I added this code:
    Code:
    Dim strSourceFilename As String = OpenFileDialog.FileName
    As a result, strSourceFilename will show the file path. For example:
    C:\Documents and Settings\Tan\My Documents\Trainee\Payroll System\frmMain.vb
    How can I shorten the file path to look something like this:
    C:\Documents and Settings\.....\Payroll System\frmMain.vb
    I just want the string in the middle part be replaced by ".....", while the front part and the end part (file name) remain unchanged.

    How can I make it? please guide me. thank you.
    Last edited by albertlse; Aug 21st, 2003 at 08:09 PM.

  2. #2
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985
    Use a regex statement to split the filename into an array by seperating them with \

    Then take the first 2 or how many in the array, put them into a string with slashes(since regex took them out)

    Then add in the dots (....) to the string

    Then take the last item in the array and add it to the end of that string.

    Hope that helps

  3. #3

    Thread Starter
    Registered User
    Join Date
    Apr 2003
    Location
    Klang, Selangor, Malaysia
    Posts
    163
    thanx for replying.
    is there anymore easier solutions?

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    There is an API that does exactly that, the only thing is I forgot which one. If you want to wait until Monday then I can get back to you with it. I have it at home but I just moved so I don't have internet access at home yet.

  5. #5
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Wait I found it. Its either PathCompactPath or PathCompactPathEx.

  6. #6

    Thread Starter
    Registered User
    Join Date
    Apr 2003
    Location
    Klang, Selangor, Malaysia
    Posts
    163
    Edneeis, thanx 4 ur reply.
    i find the Path Functions only works in VB 4, 5 and 6. am i right? if yes, how can it works in VB.NET?
    where can i get "shlwapi.dll"?
    pls guide thank you.

  7. #7
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    No it works fine in .NET. What do you mean where do you find shlwapi.dll? You don't need to find it its an API call you just declare the function and actually for this particular function the declaration doesn't even change for .NET.
    VB Code:
    1. Private Declare Function PathCompactPathEx Lib "shlwapi.dll" Alias "PathCompactPathExA" (ByVal pszOut As String, ByVal pszSrc As String, ByVal cchMax As Long, ByVal dwFlags As Long) As Long
    2.         Private Declare Function PathCompactPath Lib "shlwapi.dll" Alias "PathCompactPathA" (ByVal hDC As Long, ByVal pszPath As String, ByVal dx As Long) As Long
    3.  
    4.         Public Shared Function CompactEx(ByVal originalPath As String, ByVal maxChars As Long) As String
    5.             Dim buffer As New String(Char.Parse(0), 255)
    6.             PathCompactPathEx(buffer, originalPath, maxChars, 0)
    7.             Return buffer.TrimEnd
    8.         End Function

  8. #8

    Thread Starter
    Registered User
    Join Date
    Apr 2003
    Location
    Klang, Selangor, Malaysia
    Posts
    163

    Thumbs up

    thanx Edneeis

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width