Short Path Name in VB.NET? 
Author Message
 Short Path Name in VB.NET?

Anyone know how to return the short path name of a file or
folder in VB.NET

I tried using the old VB way with an API call (see below),
but this does not seem to work.

Private Declare Function GetShortPathNameA Lib "kernel32" _
   (ByVal lpszLongPath As String, ByVal lpszShortPath _
   As String, ByVal cchBuffer As Long) As Long

Public Function GetShortPathName(LongPath As String) As
String
  Dim s As String
  Dim i As Long

  i = Len(LongPath) + 1
  s = String(i, 0)
  GetShortPathNameA LongPath, s, i

  GetShortPathName = Left$(s, InStr(s, Chr$(0)) - 1)
End Function



Sun, 04 Dec 2005 20:05:03 GMT  
 Short Path Name in VB.NET?
Chris,

        You have to change the longs to integers in vb.net.  Here is how
declare the api now.

Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" _

(ByVal lpszLongPath As String, ByVal lpszShortPath As String, _

ByVal cchBuffer As Integer) As Integer

How to use it.

Dim strPath As String = Application.StartupPath

Dim strShortPath As String = Space(100)

GetShortPathName(strPath, strShortPath, 100)

TextBox1.Text = strShortPath

Ken

--------------


Quote:
> Anyone know how to return the short path name of a file or
> folder in VB.NET

> I tried using the old VB way with an API call (see below),
> but this does not seem to work.

> Private Declare Function GetShortPathNameA Lib "kernel32" _
>    (ByVal lpszLongPath As String, ByVal lpszShortPath _
>    As String, ByVal cchBuffer As Long) As Long

> Public Function GetShortPathName(LongPath As String) As
> String
>   Dim s As String
>   Dim i As Long

>   i = Len(LongPath) + 1
>   s = String(i, 0)
>   GetShortPathNameA LongPath, s, i

>   GetShortPathName = Left$(s, InStr(s, Chr$(0)) - 1)
> End Function



Sun, 04 Dec 2005 20:28:13 GMT  
 Short Path Name in VB.NET?

Ken - thanks but it still didn't work :o(

I'm gettnig a value of 0 returned and an empty string
instead of teh short path name



Sun, 04 Dec 2005 21:15:24 GMT  
 Short Path Name in VB.NET?
What is the long path you are trying convert?

Ken
------------

Quote:

> Ken - thanks but it still didn't work :o(

> I'm gettnig a value of 0 returned and an empty string
> instead of teh short path name



Sun, 04 Dec 2005 21:29:35 GMT  
 Short Path Name in VB.NET?
Whatever I try I get the same result

e.g.  
C:\Program Files\Microsoft Office\Office\WINWORD.EXE



Sun, 04 Dec 2005 21:46:34 GMT  
 Short Path Name in VB.NET?

D'OH!

Just realised I had somehow got ByRef in the function
declaration rather than ByVal

...not sure where I go this from though as I copied it
from an article somewhere on the net.

thanks anyways



Sun, 04 Dec 2005 22:01:42 GMT  
 Short Path Name in VB.NET?
Hello,


Quote:
> Anyone know how to return the short path name of a file or
> folder in VB.NET

For what reason do you need short path names?

Regards,
Herfried K. Wagner
--
MVP VB Classic, VB .NET
http://www.mvps.org/dotnet



Sun, 04 Dec 2005 22:11:56 GMT  
 Short Path Name in VB.NET?
Hey, I had this problem before too, the file must exist for this function to
work correctly.  Try it on a non existing file and an existing file to make
sure.

Nick.


Quote:
> Whatever I try I get the same result

> e.g.
> C:\Program Files\Microsoft Office\Office\WINWORD.EXE



Sun, 04 Dec 2005 22:14:06 GMT  
 Short Path Name in VB.NET?
Chris,

        It will only work with a Directory not a file.  C:\Program
Files\Microsoft Office\Office works not C:\Program Files\Microsoft
Office\Office\WINWORD.EXE

Ken
---------

Quote:
> Whatever I try I get the same result

> e.g.
> C:\Program Files\Microsoft Office\Office\WINWORD.EXE



Sun, 04 Dec 2005 22:17:27 GMT  
 Short Path Name in VB.NET?


Quote:
> Chris,

>         You have to change the longs to integers in vb.net.  Here is how
> declare the api now.

> Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA"
_

> (ByVal lpszLongPath As String, ByVal lpszShortPath As String, _

> ByVal cchBuffer As Integer) As Integer

Sorry, but you really should not use the Alias in .NET for the A/W
functions....  It makes much more sense to let the runtime determine the
correct function - you get better performance on the NT side of things
because it doesn't have to do all the useless Unicode-Ansi conversions...
Further, this function fills a buffer so you should not be passing in a
String type - this also forces the runtime to do extra work because strings
are immutable objects in .NET.  You should use System.Text.StringBuilder
anytime your passing a buffer to be filled by the API call.  So, here is the
best way to do this in .NET:

Declare Auto Function GetShortPathName Lib "kernel32" _
    (ByVal lpszLongPath As String,
     ByVal lpszShortPath As System.Text.StringBuilder, _
     ByVal ccBuffer As Integer) As Integer

Dim strPath As String = Application.StartupPath
Dim strShortPath As New System.Text.StringBuilder(260) ' make it MAX_PATH
long...

GetShortPath(strPath, strShortPath, strShortPath.Capacity)
TextBox1.Text = strShortPath.ToString()

By the way, you should probably check the return value... If the function is
successful, the return value is the number of characters, not including the
null terminator, written to the buffer.  If the function fails because your
buffer is to small, it returns the size of the needed buffer, so if the
return value is greater then strShortPath.Capacity, then you need to
increase the size of the StringBuilder.  If the function fails for anyother
reason, it returns 0.

Tom Shelton

Quote:
> How to use it.

> Dim strPath As String = Application.StartupPath

> Dim strShortPath As String = Space(100)

> GetShortPathName(strPath, strShortPath, 100)

> TextBox1.Text = strShortPath

> Ken

> --------------



> > Anyone know how to return the short path name of a file or
> > folder in VB.NET

> > I tried using the old VB way with an API call (see below),
> > but this does not seem to work.

> > Private Declare Function GetShortPathNameA Lib "kernel32" _
> >    (ByVal lpszLongPath As String, ByVal lpszShortPath _
> >    As String, ByVal cchBuffer As Long) As Long

> > Public Function GetShortPathName(LongPath As String) As
> > String
> >   Dim s As String
> >   Dim i As Long

> >   i = Len(LongPath) + 1
> >   s = String(i, 0)
> >   GetShortPathNameA LongPath, s, i

> >   GetShortPathName = Left$(s, InStr(s, Chr$(0)) - 1)
> > End Function



Sun, 04 Dec 2005 23:34:01 GMT  
 
 [ 12 post ] 

 Relevant Pages 

1. App.Path only returning short path name - Why?

2. app.path only returns short path name

3. App.Path returns "short" path name

4. Change long path name to short pathname

5. Short Path Names

6. how to get short path name

7. Getting Short Path Names from Long

8. Getting Short Path Names from Long

9. Getting Short Path Names from Long

10. Changing Long Paths to Short Paths in a 16-bit App

11. Couldn't find net path or user name

12. Get long file name from short file name

 

 
Powered by phpBB® Forum Software