VB.NET GetShortFileName Equivalent

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Allen

    VB.NET GetShortFileName Equivalent

    Greetings,
    I need to pass a file path to an application. This file path contains long
    directory and file names. The target application, pdftotext.exe, only
    accepts short directory and file names. Is there an equivalent VB.NET
    function to the GetShortFileNam e and similar functions available in VB6.
    Then I can pass the correct file path to pdftotext.exe.

    Thanks in advance
    Allen

    Private Function funConvertPDF(B yVal fFile$) As String

    'Create a new process

    Dim myProcess As New Process

    'This will break if cmd$ contains long file name. for example if fFile$ =
    "d:\Library\Tra velSystem\Trave lRequest.pdf

    Dim cmd$ = fFile & " " & Path.GetDirecto ryName(fFile) & "\Temp.txt"

    Try

    myProcess.Enabl eRaisingEvents = True

    myProcess = Process.Start(" D:\pdftotext.ex e", cmd$)

    'do not procede until the converion is complete.

    myProcess.WaitF orExit()

    'Close the process

    myProcess.Close ()

    funConvertPDF = Path.GetDirecto ryName(fFile) & "\Temp.txt"

    Catch a As Exception

    Console.WriteLi ne(a.Message)

    Finally

    End Try

    End Function



  • Tom Shelton

    #2
    Re: VB.NET GetShortFileNam e Equivalent

    Allen wrote:
    [color=blue]
    > Greetings,
    > I need to pass a file path to an application. This file path contains long
    > directory and file names. The target application, pdftotext.exe, only
    > accepts short directory and file names. Is there an equivalent VB.NET
    > function to the GetShortFileNam e and similar functions available in VB6.
    > Then I can pass the correct file path to pdftotext.exe.
    >
    > Thanks in advance
    > Allen
    >
    > Private Function funConvertPDF(B yVal fFile$) As String
    >
    > 'Create a new process
    >
    > Dim myProcess As New Process
    >
    > 'This will break if cmd$ contains long file name. for example if fFile$ =
    > "d:\Library\Tra velSystem\Trave lRequest.pdf
    >
    > Dim cmd$ = fFile & " " & Path.GetDirecto ryName(fFile) & "\Temp.txt"
    >
    > Try
    >
    > myProcess.Enabl eRaisingEvents = True
    >
    > myProcess = Process.Start(" D:\pdftotext.ex e", cmd$)
    >
    > 'do not procede until the converion is complete.
    >
    > myProcess.WaitF orExit()
    >
    > 'Close the process
    >
    > myProcess.Close ()
    >
    > funConvertPDF = Path.GetDirecto ryName(fFile) & "\Temp.txt"
    >
    > Catch a As Exception
    >
    > Console.WriteLi ne(a.Message)
    >
    > Finally
    >
    > End Try
    >
    > End Function
    >
    >
    >[/color]

    I don't know of a .NET equivalent, but you can continue to call
    GetShortPathNam e from VB.NET through P/Invoke, something like the
    following air code:

    Private Const MAX_PATH As Integer = 260

    Private Declare Auto Function GetShortPathNam e Lib "kernel32" ( _
    ByVal lpszLongPath As String, _
    ByVal lpszShortPath As System.Text.Str ingBuilder, _
    ByVal cchBuffer As Integer) As Integer


    Public Function GetShortFileNam e(ByVal LongPath As String) As String
    Dim ShortPath As New System.Text.Str ingBuilder(MAX_ PATH)

    Dim BufferSize As Integer = GetShortPathNam e( _
    LongPath,
    ShortPath,
    ShortPath.Capac ity)

    ' You might want to check the return value here
    ' If BufferSize is greater then ShortPath.Capac ity then
    ' you need to reallocate the stringbuilder to BufferSize + 1
    ' and call GetShortPathNam e again. If the return value is 0
    ' then you will want to generate an error.

    Return ShortPath.ToStr ing()
    End Function

    HTH,
    Tom Shelton

    Comment

    • Ken Tucker

      #3
      Re: VB.NET GetShortFileNam e Equivalent

      Hi,

      Use the get filename api.

      API Declare

      Declare Function GetShortPathNam e Lib "kernel32" Alias "GetShortPathNa meA" _

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

      ByVal cchBuffer As Integer) As Integer

      Example

      Dim strPath As String = Application.Sta rtupPath

      Dim strShortPath As String = Space(100)

      GetShortPathNam e(strPath, strShortPath, 100)

      TextBox1.Text = strShortPath

      Ken

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

      "Allen" <gt5092a@hotmai l.com> wrote in message
      news:%23oeVOkfg DHA.1440@TK2MSF TNGP12.phx.gbl. ..[color=blue]
      > Greetings,
      > I need to pass a file path to an application. This file path contains[/color]
      long[color=blue]
      > directory and file names. The target application, pdftotext.exe, only
      > accepts short directory and file names. Is there an equivalent VB.NET
      > function to the GetShortFileNam e and similar functions available in VB6.
      > Then I can pass the correct file path to pdftotext.exe.
      >
      > Thanks in advance
      > Allen
      >
      > Private Function funConvertPDF(B yVal fFile$) As String
      >
      > 'Create a new process
      >
      > Dim myProcess As New Process
      >
      > 'This will break if cmd$ contains long file name. for example if fFile$[/color]
      =[color=blue]
      > "d:\Library\Tra velSystem\Trave lRequest.pdf
      >
      > Dim cmd$ = fFile & " " & Path.GetDirecto ryName(fFile) & "\Temp.txt"
      >
      > Try
      >
      > myProcess.Enabl eRaisingEvents = True
      >
      > myProcess = Process.Start(" D:\pdftotext.ex e", cmd$)
      >
      > 'do not procede until the converion is complete.
      >
      > myProcess.WaitF orExit()
      >
      > 'Close the process
      >
      > myProcess.Close ()
      >
      > funConvertPDF = Path.GetDirecto ryName(fFile) & "\Temp.txt"
      >
      > Catch a As Exception
      >
      > Console.WriteLi ne(a.Message)
      >
      > Finally
      >
      > End Try
      >
      > End Function
      >
      >
      >[/color]


      Comment

      • Herfried K. Wagner [MVP]

        #4
        Re: VB.NET GetShortFileNam e Equivalent

        Hello,

        "Ken Tucker" <vb2ae@bellsout h.net> schrieb:[color=blue]
        > Use the get filename api.[/color]

        \\\
        Private Declare Auto Function GetShortPathNam e Lib "kernel32.d ll" ( _
        ByVal lpszLongPath As String, _
        ByVal lpszShortPath As String, _
        ByVal cchBuffer As Int32 _
        ) As Int32
        ..
        ..
        ..
        Dim strPath As String = Application.Sta rtupPath
        Dim strShortPath As String = Space(100)
        Dim n As Int32 = GetShortPathNam e(strPath, strShortPath, 100)
        MsgBox(Strings. Left(strShortPa th, n))
        ///

        --
        Herfried K. Wagner
        MVP · VB Classic, VB.NET
        Die Website von H. Wagner zu .NET, Visual Basic .NET, Classic Visual Basic, Webentwicklung und mehr.



        Comment

        • Tom Shelton

          #5
          Re: VB.NET GetShortFileNam e Equivalent

          Herfried K. Wagner [MVP] wrote:[color=blue]
          > Hello,
          >
          > "Ken Tucker" <vb2ae@bellsout h.net> schrieb:
          >[color=green]
          >> Use the get filename api.[/color]
          >
          >
          > \\\
          > Private Declare Auto Function GetShortPathNam e Lib "kernel32.d ll" ( _
          > ByVal lpszLongPath As String, _
          > ByVal lpszShortPath As String, _
          > ByVal cchBuffer As Int32 _
          > ) As Int32
          > .[/color]

          Sorry, Herfried - but you should use a StringBuilder for the
          lpszShortPath parameter:

          Private Declare Auto Function GetShortPathNam e Lib "kernel32" ( _
          ByVal lpszLongPath As String, _
          ByVal lpszShortPath As System.Text.Str ingBuilder, _
          ByVal cchBuffer As Int32) As Int32


          Dim strPath As String = Application.Sta rtupPath
          Dim strShortPath As New System.Text.Str ingBuilder(260) ' MaxPath
          Dim n As Int32 = GetShortPathNam e(strPath, strShortPath,
          stShortPath.Cap acity)
          MsgBox(strShort Path.ToString() )

          Using strings for buffers is bad for performance considering the
          immutable nature of strings...

          Tom Shelton

          Comment

          • Herfried K. Wagner [MVP]

            #6
            Re: VB.NET GetShortFileNam e Equivalent

            Hello,

            "Tom Shelton" <tom@mtogden.co m> schrieb:[color=blue][color=green]
            > > \\\
            > > Private Declare Auto Function GetShortPathNam e Lib "kernel32.d ll" ( _
            > > ByVal lpszLongPath As String, _
            > > ByVal lpszShortPath As String, _
            > > ByVal cchBuffer As Int32 _
            > > ) As Int32
            > > .[/color]
            >
            > Sorry, Herfried - but you should use a StringBuilder for the
            > lpszShortPath parameter:
            >
            > Private Declare Auto Function GetShortPathNam e Lib "kernel32" ( _
            > ByVal lpszLongPath As String, _
            > ByVal lpszShortPath As System.Text.Str ingBuilder, _
            > ByVal cchBuffer As Int32) As Int32
            >
            >
            > Dim strPath As String = Application.Sta rtupPath
            > Dim strShortPath As New System.Text.Str ingBuilder(260) ' MaxPath
            > Dim n As Int32 = GetShortPathNam e(strPath, strShortPath,
            > stShortPath.Cap acity)
            > MsgBox(strShort Path.ToString() )
            >
            > Using strings for buffers is bad for performance considering the
            > immutable nature of strings...[/color]

            Are you really sure the 'StringBuilder' will have a better performance when
            the function is called once?

            ;-)

            --
            Herfried K. Wagner
            MVP · VB Classic, VB.NET
            Die Website von H. Wagner zu .NET, Visual Basic .NET, Classic Visual Basic, Webentwicklung und mehr.



            Comment

            • Cor

              #7
              Re: VB.NET GetShortFileNam e Equivalent

              Hi Herfried,
              Stringbuilder is preferable.
              :-)))))))
              Cor


              Comment

              • Herfried K. Wagner [MVP]

                #8
                Re: VB.NET GetShortFileNam e Equivalent

                Hello,

                "Cor" <non@non.com> schrieb:[color=blue]
                > Stringbuilder is preferable.
                > :-)))))))[/color]

                Why?!

                --
                Herfried K. Wagner
                MVP · VB Classic, VB.NET
                Die Website von H. Wagner zu .NET, Visual Basic .NET, Classic Visual Basic, Webentwicklung und mehr.



                Comment

                • Cor

                  #9
                  Re: VB.NET GetShortFileNam e Equivalent

                  Herfried,
                  Just because you said that in the other message about a string handling to
                  me.
                  But I tested it, when you want to have the test, I do it in the next reply.
                  I don't know and if it affects this question.
                  But I thought before testing, it would be nothing but concatenating a string
                  traditional way can be more than 100 times slower than stringbuilder.
                  I did not even look good if this question was concatentating a string (I
                  thought in a flash to see it).
                  (That has always been a problem with memory allocation).

                  When it in this routine just for one occurence, you know what I always say
                  about that.
                  "Do it in the way you like the most".
                  Cor


                  Comment

                  • Tom Shelton

                    #10
                    Re: VB.NET GetShortFileNam e Equivalent

                    Herfried K. Wagner [MVP] wrote:
                    [color=blue]
                    > Hello,
                    >
                    > "Tom Shelton" <tom@mtogden.co m> schrieb:
                    >[color=green][color=darkred]
                    >>>\\\
                    >>>Private Declare Auto Function GetShortPathNam e Lib "kernel32.d ll" ( _
                    >>> ByVal lpszLongPath As String, _
                    >>> ByVal lpszShortPath As String, _
                    >>> ByVal cchBuffer As Int32 _
                    >>>) As Int32
                    >>>.[/color]
                    >>
                    >>Sorry, Herfried - but you should use a StringBuilder for the
                    >>lpszShortPa th parameter:
                    >>
                    >>Private Declare Auto Function GetShortPathNam e Lib "kernel32" ( _
                    >>ByVal lpszLongPath As String, _
                    >>ByVal lpszShortPath As System.Text.Str ingBuilder, _
                    >>ByVal cchBuffer As Int32) As Int32
                    >>
                    >>
                    >>Dim strPath As String = Application.Sta rtupPath
                    >>Dim strShortPath As New System.Text.Str ingBuilder(260) ' MaxPath
                    >>Dim n As Int32 = GetShortPathNam e(strPath, strShortPath,
                    >>stShortPath.C apacity)
                    >>MsgBox(strSho rtPath.ToString ())
                    >>
                    >>Using strings for buffers is bad for performance considering the
                    >>immutable nature of strings...[/color]
                    >
                    >
                    > Are you really sure the 'StringBuilder' will have a better performance when
                    > the function is called once?
                    >
                    > ;-)
                    >[/color]

                    No... It won't make a difference when the function is only called once -
                    or if it is called infrequently. But, I suppose I pickup on it because
                    I think it is a bad habbit to use the String type for return buffers.
                    Basically, because there are situations where the extra work - and
                    memory (since it can cause several temporary string objects to be
                    created) - can be detrimental. IMHO, it is always preferable to use a
                    StringBuilder when passing string buffers to unmanaged code...

                    You might say it is one of my P/Invoke pet peve's...
                    1. Using Alias for A/W functions - what's up with that?
                    2. Using As String for string buffers....
                    ....

                    Tom Shelton

                    Comment

                    • Herfried K. Wagner [MVP]

                      #11
                      Re: VB.NET GetShortFileNam e Equivalent

                      Hello,

                      "Cor" <non@non.com> schrieb:[color=blue]
                      > Just because you said that in the other message about a
                      > string handling to me.[/color]

                      In this case I think that the datatypes will me marshalled automatically and
                      the performance won't be that bad. In case of your sample you are
                      concatenating strings for example > 100 times. This can be very time
                      costly.

                      --
                      Herfried K. Wagner
                      MVP · VB Classic, VB.NET
                      Die Website von H. Wagner zu .NET, Visual Basic .NET, Classic Visual Basic, Webentwicklung und mehr.



                      Comment

                      • Tom Shelton

                        #12
                        Re: VB.NET GetShortFileNam e Equivalent

                        Herfried K. Wagner [MVP] wrote:
                        [color=blue]
                        > Hello,
                        >
                        > "Cor" <non@non.com> schrieb:
                        >[color=green]
                        >>Just because you said that in the other message about a
                        >>string handling to me.[/color]
                        >
                        >
                        > In this case I think that the datatypes will me marshalled automatically and
                        > the performance won't be that bad. In case of your sample you are
                        > concatenating strings for example > 100 times. This can be very time
                        > costly.
                        >[/color]

                        Maybe not AS bad - but still, it is a bad habbit. The marshaller has to
                        work much harder marshaling String then it does with StringBuilder. It
                        isn't as though it is harder to use StringBuilder - in fact it is often
                        easier. Personally, if a buffer is going to be modified - using a
                        stringbuilder is the best way.

                        Tom Shelton

                        Comment

                        • Michael \(michka\) Kaplan [MS]

                          #13
                          Re: VB.NET GetShortFileNam e Equivalent


                          "Tom Shelton" <tom@mtogden.co m> wrote...

                          [color=blue]
                          > You might say it is one of my P/Invoke pet peve's...
                          > 1. Using Alias for A/W functions - what's up with that?[/color]

                          Why? I mean, why is it a problem for you in C#?
                          [color=blue]
                          > 2. Using As String for string buffers....[/color]

                          Why? Strings are immutable -- so it is not surprising that you are not
                          allowed to use them in contexts that would mute them....


                          --
                          MichKa [MS]

                          This posting is provided "AS IS" with
                          no warranties, and confers no rights.



                          Comment

                          • Michael \(michka\) Kaplan [MS]

                            #14
                            Re: VB.NET GetShortFileNam e Equivalent

                            "Tom Shelton" <tom@mtogden.co m> wrote...
                            [color=blue]
                            > Good:
                            >
                            > Declare Auto Function GetUserName Lib "advapi32" ( _
                            > ByVal lpBuffer As System.Text.Str ingBuilder, _
                            > ByRef nSize As Integer) As Boolean
                            >
                            > Bad:
                            >
                            > Declare Function GetUserName Lib "advapi32" Alias "GetUserNam eA" ( _
                            > ByVal lpBuffer As String, _
                            > ByRef nSize As Integer) As Boolean
                            >
                            >
                            > That make it clear? In other words, leave off the Alias and use the
                            > Auto keyword.[/color]

                            Thats one option -- but it ignores the ton of legacy Declares that use the
                            old syntax. If you want to update, why not get away from declares and use
                            the more feature filled syntax that avoids them entirely?


                            --
                            MichKa [MS]

                            This posting is provided "AS IS" with
                            no warranties, and confers no rights.



                            Comment

                            • Tom Shelton

                              #15
                              Re: VB.NET GetShortFileNam e Equivalent

                              In article <#N#hc8BiDHA.16 96@TK2MSFTNGP09 .phx.gbl>, Michael (michka) Kaplan [MS] wrote:[color=blue]
                              > "Tom Shelton" <tom@mtogden.co m> wrote...
                              >[color=green]
                              >> Good:
                              >>
                              >> Declare Auto Function GetUserName Lib "advapi32" ( _
                              >> ByVal lpBuffer As System.Text.Str ingBuilder, _
                              >> ByRef nSize As Integer) As Boolean
                              >>
                              >> Bad:
                              >>
                              >> Declare Function GetUserName Lib "advapi32" Alias "GetUserNam eA" ( _
                              >> ByVal lpBuffer As String, _
                              >> ByRef nSize As Integer) As Boolean
                              >>
                              >>
                              >> That make it clear? In other words, leave off the Alias and use the
                              >> Auto keyword.[/color]
                              >
                              > Thats one option -- but it ignores the ton of legacy Declares that use the
                              > old syntax. If you want to update, why not get away from declares and use
                              > the more feature filled syntax that avoids them entirely?
                              >
                              >[/color]

                              Personally, I agree. I like the new syntax better and it does allow
                              more complete control. But, it is over kill for the majority of API
                              calls, and the declare syntax isn't quite as confusing for people
                              comming from a VB background. The new syntax doesn't bother me, because
                              it is essentially the same method that C# uses - and since I do almost
                              all my development in C#, I'm used to it. Though, I think it is a good
                              idea for people to look into using DllImport - since it seems to me I've
                              come across a couple of situations where it was required to get the
                              marshalling right (though, I can't remember exactly what they were off
                              the top of my head...).

                              Tom Shelton

                              Comment

                              Working...