469,326 Members | 1,351 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,326 developers. It's quick & easy.

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

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
Nov 17 '05 #1
3 6248
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/...tShortPathName
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

Nov 17 '05 #2
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 Flanagan wrote:
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/...tShortPathName
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

Nov 17 '05 #3
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 Si wrote:
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 Flanagan wrote:
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/...tShortPathName
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.FileSystemObjec t")" 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

Nov 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Eric Cory | last post: by
9 posts views Thread by JimmyKoolPantz | last post: by
6 posts views Thread by saif.shakeel | last post: by
7 posts views Thread by saif.shakeel | last post: by
butro
2 posts views Thread by butro | last post: by
2 posts views Thread by pmlane2001 | last post: by
1 post views Thread by =?Utf-8?B?RVFOaXNo?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.