.net里长短文件名的解决办法

当我用Path.GetTempFileName()函数去取一个目录名时,竟然得到这样的结果:C:\DOCUME~1\Vitami~1。。。应该是c:\Documents and Settings\VitaminC.net的。找了半天,最后在GotDotNet找到了结果,把代码贴出来大家看看:

None.gifusing  System;
None.gif
using
 System.Text;
None.gif
using
 System.Runtime.InteropServices;
None.gif
None.gif
namespace
 ShellPathNameConvert
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**/
/// <summary>
InBlock.gif    
/// Converts file and directory paths to their respective
InBlock.gif    
///
 long and short name versions.
InBlock.gif    
/// </summary>

ExpandedSubBlockEnd.gif    
/// <remarks>This class uses InteropServices to call GetLongPathName and GetShortPathName</remarks>

InBlock.gif    public class Convert
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif
{
InBlock.gif        [DllImport(
"kernel32.dll"
)]
InBlock.gif        
static extern uint GetLongPathName(string
 shortname, StringBuilder
InBlock.gif            longnamebuff, 
uint
 buffersize);
InBlock.gif
InBlock.gif        [DllImport(
"kernel32.dll", CharSet =
 CharSet.Auto)]
InBlock.gif        
public static extern int
 GetShortPathName(
InBlock.gif            [MarshalAs(UnmanagedType.LPTStr)]
InBlock.gif            
string
 path,
InBlock.gif            [MarshalAs(UnmanagedType.LPTStr)]
InBlock.gif            StringBuilder shortPath,
InBlock.gif            
int
 shortPathLength);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**/
/// <summary>
InBlock.gif        
/// The ToShortPathNameToLongPathName function retrieves the long path form of a specified short input path
InBlock.gif        
/// </summary>

InBlock.gif        
/// <param name="shortName">The short name path</param>
ExpandedSubBlockEnd.gif        
/// <returns>A long name path string</returns>

InBlock.gif        public static string ToLongPathName(string shortName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
{
InBlock.gif            StringBuilder longNameBuffer 
= new StringBuilder(256
);
InBlock.gif            
uint bufferSize = (uint
)longNameBuffer.Capacity;
InBlock.gif
InBlock.gif            GetLongPathName(shortName, longNameBuffer, bufferSize);
InBlock.gif
InBlock.gif            
return
 longNameBuffer.ToString();
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// The ToLongPathNameToShortPathName function retrieves the short path form of a specified long input path
InBlock.gif        
/// </summary>

InBlock.gif        
/// <param name="longName">The long name path</param>
ExpandedSubBlockEnd.gif        
/// <returns>A short name path string</returns>

InBlock.gif        public static string ToShortPathName(string longName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
{
InBlock.gif            StringBuilder shortNameBuffer 
= new StringBuilder(256
);
InBlock.gif            
int bufferSize =
 shortNameBuffer.Capacity;
InBlock.gif
InBlock.gif            
int result =
 GetShortPathName(longName, shortNameBuffer, bufferSize);
InBlock.gif
InBlock.gif            
return
 shortNameBuffer.ToString();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

运行结果:
Current directory:
D:\Documents and Settings\Administrator\桌面\ShellPathNameConvert\ShellPathNameC
onvertTest\bin\Debug

Short path name:
D:\DOCUME~1\ADMINI~1\桌面\SHELLP~1\SHELLP~2\bin\Debug

Long path name:
D:\Documents and Settings\Administrator\桌面\ShellPathNameConvert\ShellPathNameC
onvertTest\bin\Debug

附带上作者的说明:
ShellPathNameConvert will allow you to convert to and from Long and Short paths. It's common for the Windows shell to send you a short path if your application takes command line arguments. However, it's not always convenient to work with the short path names and .NET provides no built in way to get the long path nam

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值