473,399 Members | 3,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

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 GetShortFileName and similar functions available in VB6.
Then I can pass the correct file path to pdftotext.exe.

Thanks in advance
Allen

Private Function funConvertPDF(ByVal 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\TravelSystem\TravelRequest.pdf

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

Try

myProcess.EnableRaisingEvents = True

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

'do not procede until the converion is complete.

myProcess.WaitForExit()

'Close the process

myProcess.Close()

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

Catch a As Exception

Console.WriteLine(a.Message)

Finally

End Try

End Function

Nov 20 '05 #1
14 10796
Allen wrote:
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 GetShortFileName and similar functions available in VB6.
Then I can pass the correct file path to pdftotext.exe.

Thanks in advance
Allen

Private Function funConvertPDF(ByVal 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\TravelSystem\TravelRequest.pdf

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

Try

myProcess.EnableRaisingEvents = True

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

'do not procede until the converion is complete.

myProcess.WaitForExit()

'Close the process

myProcess.Close()

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

Catch a As Exception

Console.WriteLine(a.Message)

Finally

End Try

End Function


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

Private Const MAX_PATH As Integer = 260

Private Declare Auto Function GetShortPathName Lib "kernel32" ( _
ByVal lpszLongPath As String, _
ByVal lpszShortPath As System.Text.StringBuilder, _
ByVal cchBuffer As Integer) As Integer
Public Function GetShortFileName(ByVal LongPath As String) As String
Dim ShortPath As New System.Text.StringBuilder(MAX_PATH)

Dim BufferSize As Integer = GetShortPathName( _
LongPath,
ShortPath,
ShortPath.Capacity)

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

Return ShortPath.ToString()
End Function

HTH,
Tom Shelton

Nov 20 '05 #2
Hi,

Use the get filename api.

API Declare

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

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

ByVal cchBuffer As Integer) As Integer

Example

Dim strPath As String = Application.StartupPath

Dim strShortPath As String = Space(100)

GetShortPathName(strPath, strShortPath, 100)

TextBox1.Text = strShortPath

Ken

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

"Allen" <gt*****@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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 GetShortFileName and similar functions available in VB6.
Then I can pass the correct file path to pdftotext.exe.

Thanks in advance
Allen

Private Function funConvertPDF(ByVal 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\TravelSystem\TravelRequest.pdf

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

Try

myProcess.EnableRaisingEvents = True

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

'do not procede until the converion is complete.

myProcess.WaitForExit()

'Close the process

myProcess.Close()

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

Catch a As Exception

Console.WriteLine(a.Message)

Finally

End Try

End Function

Nov 20 '05 #3
Hello,

"Ken Tucker" <vb***@bellsouth.net> schrieb:
Use the get filename api.


\\\
Private Declare Auto Function GetShortPathName Lib "kernel32.dll" ( _
ByVal lpszLongPath As String, _
ByVal lpszShortPath As String, _
ByVal cchBuffer As Int32 _
) As Int32
..
..
..
Dim strPath As String = Application.StartupPath
Dim strShortPath As String = Space(100)
Dim n As Int32 = GetShortPathName(strPath, strShortPath, 100)
MsgBox(Strings.Left(strShortPath, n))
///

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
http://www.mvps.org/dotnet
Nov 20 '05 #4
Herfried K. Wagner [MVP] wrote:
Hello,

"Ken Tucker" <vb***@bellsouth.net> schrieb:
Use the get filename api.

\\\
Private Declare Auto Function GetShortPathName Lib "kernel32.dll" ( _
ByVal lpszLongPath As String, _
ByVal lpszShortPath As String, _
ByVal cchBuffer As Int32 _
) As Int32
.


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

Private Declare Auto Function GetShortPathName Lib "kernel32" ( _
ByVal lpszLongPath As String, _
ByVal lpszShortPath As System.Text.StringBuilder, _
ByVal cchBuffer As Int32) As Int32
Dim strPath As String = Application.StartupPath
Dim strShortPath As New System.Text.StringBuilder(260) ' MaxPath
Dim n As Int32 = GetShortPathName(strPath, strShortPath,
stShortPath.Capacity)
MsgBox(strShortPath.ToString())

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

Tom Shelton

Nov 20 '05 #5
Hello,

"Tom Shelton" <to*@mtogden.com> schrieb:
\\\
Private Declare Auto Function GetShortPathName Lib "kernel32.dll" ( _
ByVal lpszLongPath As String, _
ByVal lpszShortPath As String, _
ByVal cchBuffer As Int32 _
) As Int32
.


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

Private Declare Auto Function GetShortPathName Lib "kernel32" ( _
ByVal lpszLongPath As String, _
ByVal lpszShortPath As System.Text.StringBuilder, _
ByVal cchBuffer As Int32) As Int32
Dim strPath As String = Application.StartupPath
Dim strShortPath As New System.Text.StringBuilder(260) ' MaxPath
Dim n As Int32 = GetShortPathName(strPath, strShortPath,
stShortPath.Capacity)
MsgBox(strShortPath.ToString())

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


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
http://www.mvps.org/dotnet
Nov 20 '05 #6
Cor
Hi Herfried,
Stringbuilder is preferable.
:-)))))))
Cor
Nov 20 '05 #7
Hello,

"Cor" <no*@non.com> schrieb:
Stringbuilder is preferable.
:-)))))))


Why?!

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
http://www.mvps.org/dotnet
Nov 20 '05 #8
Cor
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
Nov 20 '05 #9
Herfried K. Wagner [MVP] wrote:
Hello,

"Tom Shelton" <to*@mtogden.com> schrieb:
\\\
Private Declare Auto Function GetShortPathName Lib "kernel32.dll" ( _
ByVal lpszLongPath As String, _
ByVal lpszShortPath As String, _
ByVal cchBuffer As Int32 _
) As Int32
.


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

Private Declare Auto Function GetShortPathName Lib "kernel32" ( _
ByVal lpszLongPath As String, _
ByVal lpszShortPath As System.Text.StringBuilder, _
ByVal cchBuffer As Int32) As Int32
Dim strPath As String = Application.StartupPath
Dim strShortPath As New System.Text.StringBuilder(260) ' MaxPath
Dim n As Int32 = GetShortPathName(strPath, strShortPath,
stShortPath.Capacity)
MsgBox(strShortPath.ToString())

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

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

;-)


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

Nov 20 '05 #10
Hello,

"Cor" <no*@non.com> schrieb:
Just because you said that in the other message about a
string handling to me.


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
http://www.mvps.org/dotnet
Nov 20 '05 #11
Herfried K. Wagner [MVP] wrote:
Hello,

"Cor" <no*@non.com> schrieb:
Just because you said that in the other message about a
string handling to me.

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.


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

Nov 20 '05 #12

"Tom Shelton" <to*@mtogden.com> wrote...

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?
Why? I mean, why is it a problem for you in C#?
2. Using As String for string buffers....


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.

Nov 20 '05 #13
"Tom Shelton" <to*@mtogden.com> wrote...
Good:

Declare Auto Function GetUserName Lib "advapi32" ( _
ByVal lpBuffer As System.Text.StringBuilder, _
ByRef nSize As Integer) As Boolean

Bad:

Declare Function GetUserName Lib "advapi32" Alias "GetUserNameA" ( _
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.


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.

Nov 20 '05 #14
In article <#N**************@TK2MSFTNGP09.phx.gbl>, Michael (michka) Kaplan [MS] wrote:
"Tom Shelton" <to*@mtogden.com> wrote...
Good:

Declare Auto Function GetUserName Lib "advapi32" ( _
ByVal lpBuffer As System.Text.StringBuilder, _
ByRef nSize As Integer) As Boolean

Bad:

Declare Function GetUserName Lib "advapi32" Alias "GetUserNameA" ( _
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.


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?


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
Nov 20 '05 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: John | last post by:
Is there an equivalent of COM on Linux that I can get through Python. My need is to have some sort of language independent component framework. I can think of CORBA but I have to have a server...
2
by: Michael Foord | last post by:
Please pardon my ignorance on this one - but I'm not certain how the sign bt is treated in python bitwise operators. I've trying to convert a javascript DES encryption routine into python. ...
3
by: Robert Dodier | last post by:
Hello, Here's a thought that I'm sure has already occurred to someone else, I just can't find any record of it yet. An XML document is just a more verbose and clumsy representation of an...
1
by: Vannela | last post by:
Is there any equivalent control in .NET for the Power builder DataWindow control? I am explaining the Datawindow architecture to some extent. Power Builder DataWindow Control has got different...
6
by: Frank Rachel | last post by:
So I can focus on the correct areas of research, I was wondering if someone could give me the .NET equivelents for this J2EE architecture: JSP's make calls to local JavaBean Controls. The...
7
by: Tim Conner | last post by:
Hi, I am an ex-delphi programmer, and I having a real hard time with the following simple code (example ): Which is the equivalent to the following code ? var chars : PChar; sBack, s :...
10
by: karch | last post by:
How would this C# contruct be represented in C++/CLI? Or is it even possible? PolicyLevel level = (enumerator->Current) as PolicyLevel; Thanks, Karch
9
by: Alan Silver | last post by:
Hello, I'm converting some old VB6 code to use with ASP.NET and have come unstuck with the Asc() function. This was used in the old VB6 code to convert a character to its ASCII numeric...
14
by: grid | last post by:
Hi, I have a certain situation where a particular piece of code works on a particular compiler but fails on another proprietary compiler.It seems to have been fixed but I just want to confirm if...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.