How can I get the File shortName in 8.3 format using C#

  • Thread starter Thread starter Jingnan Si
  • Start date Start date
J

Jingnan Si

Hi,
I am porting a old application from Asp, in the old code, I am using
"Server.CreateObject("Scripting.FileSystemObject")" to get the file
short name in 8.3 format, but I am wondering if there is pure C# way to
get the file short name instead of using ActiveX object?

Thanks
-Si Jingnan
 
There isn't a BCL method that I know of, but P/Invoke is probably less
clunky than doing COM interop and creating a dependency on the Scripting
library.

Add the following declaration to your code, and then call GetShortPathName()

[DllImport("kernel32.dll")]
static extern uint GetShortPathName(string lpszLongPath,
[Out] StringBuilder lpszShortPath, uint cchBuffer);

Taken from (see for sample code):
http://www.pinvoke.net/default.aspx/kernel32.GetShortPathName


Joshua Flanagan
http://flimflan.com/blog
 
Thanks, it works, but a little bit problem, the API can only return the
short name for a exist file (not a big problem).

-Si Jingnan
Joshua said:
There isn't a BCL method that I know of, but P/Invoke is probably less
clunky than doing COM interop and creating a dependency on the Scripting
library.

Add the following declaration to your code, and then call GetShortPathName()

[DllImport("kernel32.dll")]
static extern uint GetShortPathName(string lpszLongPath,
[Out] StringBuilder lpszShortPath, uint cchBuffer);

Taken from (see for sample code):
http://www.pinvoke.net/default.aspx/kernel32.GetShortPathName


Joshua Flanagan
http://flimflan.com/blog


Jingnan said:
Hi,
I am porting a old application from Asp, in the old code, I am using
"Server.CreateObject("Scripting.FileSystemObject")" to get the file
short name in 8.3 format, but I am wondering if there is pure C# way to
get the file short name instead of using ActiveX object?

Thanks
-Si Jingnan
 
Yes, I think that restriction is necessary.
If the Scripting.FileSystemObject gives you the shortname for a file
that does not exist, I don't see how it could be reliable.
The shortname for a file depends on the other files in the filesystem at
the the time file is created. For example, if you have a file named:
MyReallyLongFilename.txt
and create it in an empty directory, its shortname will be:
MyReal~1.txt

However, if you created that same file in a directory that already
contained a file named:
MyReallyGoodNovel.txt
your file would be named
MyReal~2.txt

Two different possible shortnames for the exact same filename. It
depends on the other files in the folder.


Jingnan said:
Thanks, it works, but a little bit problem, the API can only return the
short name for a exist file (not a big problem).

-Si Jingnan
Joshua said:
There isn't a BCL method that I know of, but P/Invoke is probably less
clunky than doing COM interop and creating a dependency on the Scripting
library.

Add the following declaration to your code, and then call GetShortPathName()

[DllImport("kernel32.dll")]
static extern uint GetShortPathName(string lpszLongPath,
[Out] StringBuilder lpszShortPath, uint cchBuffer);

Taken from (see for sample code):
http://www.pinvoke.net/default.aspx/kernel32.GetShortPathName


Joshua Flanagan
http://flimflan.com/blog


Jingnan Si wrote:

Hi,
I am porting a old application from Asp, in the old code, I am using
"Server.CreateObject("Scripting.FileSystemObject")" to get the file
short name in 8.3 format, but I am wondering if there is pure C# way to
get the file short name instead of using ActiveX object?

Thanks
-Si Jingnan
 
Back
Top