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

2023-11-21 07:10

本文主要是介绍.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

这篇关于.net里长短文件名的解决办法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/400689

相关文章

Go标准库常见错误分析和解决办法

《Go标准库常见错误分析和解决办法》Go语言的标准库为开发者提供了丰富且高效的工具,涵盖了从网络编程到文件操作等各个方面,然而,标准库虽好,使用不当却可能适得其反,正所谓工欲善其事,必先利其器,本文将... 目录1. 使用了错误的time.Duration2. time.After导致的内存泄漏3. jsO

springboot循环依赖问题案例代码及解决办法

《springboot循环依赖问题案例代码及解决办法》在SpringBoot中,如果两个或多个Bean之间存在循环依赖(即BeanA依赖BeanB,而BeanB又依赖BeanA),会导致Spring的... 目录1. 什么是循环依赖?2. 循环依赖的场景案例3. 解决循环依赖的常见方法方法 1:使用 @La

MySQL错误代码2058和2059的解决办法

《MySQL错误代码2058和2059的解决办法》:本文主要介绍MySQL错误代码2058和2059的解决办法,2058和2059的错误码核心都是你用的客户端工具和mysql版本的密码插件不匹配,... 目录1. 前置理解2.报错现象3.解决办法(敲重点!!!)1. php前置理解2058和2059的错误

Docker镜像pull失败两种解决办法小结

《Docker镜像pull失败两种解决办法小结》有时候我们在拉取Docker镜像的过程中会遇到一些问题,:本文主要介绍Docker镜像pull失败两种解决办法的相关资料,文中通过代码介绍的非常详细... 目录docker 镜像 pull 失败解决办法1DrQwWCocker 镜像 pull 失败解决方法2总

关于Docker Desktop的WSL报错问题解决办法

《关于DockerDesktop的WSL报错问题解决办法》:本文主要介绍关于DockerDesktop的WSL报错问题解决办法的相关资料,排查发现是因清理%temp%文件夹误删关键WSL文件,... 目录发现问题排查过程:解决方法其实很简单:重装之后再看就能够查到了:最后分享几个排查这类问题的小www.cp

Linux文件名修改方法大全

《Linux文件名修改方法大全》在Linux系统中,文件名修改是一个常见且重要的操作,文件名修改可以更好地管理文件和文件夹,使其更具可读性和有序性,本文将介绍三种在Linux系统下常用的文件名修改方法... 目录一、引言二、使用mv命令修改文件名三、使用rename命令修改文件名四、mv命令和rename命

电脑开机提示krpt.dll丢失怎么解决? krpt.dll文件缺失的多种解决办法

《电脑开机提示krpt.dll丢失怎么解决?krpt.dll文件缺失的多种解决办法》krpt.dll是Windows操作系统中的一个动态链接库文件,它对于系统的正常运行起着重要的作用,本文将详细介绍... 在使用 Windows 操作系统的过程中,用户有时会遇到各种错误提示,其中“找不到 krpt.dll”

Pycharm安装报错:Cannot detect a launch configuration解决办法

《Pycharm安装报错:Cannotdetectalaunchconfiguration解决办法》本文主要介绍了Pycharm安装报错:Cannotdetectalaunchconfigur... 本文主要介绍了Pycharm安装报错:Cannot detect a launch configuratio

基于.NET编写工具类解决JSON乱码问题

《基于.NET编写工具类解决JSON乱码问题》在开发过程中,我们经常会遇到JSON数据处理的问题,尤其是在数据传输和解析过程中,很容易出现编码错误导致的乱码问题,下面我们就来编写一个.NET工具类来解... 目录问题背景核心原理工具类实现使用示例总结在开发过程中,我们经常会遇到jsON数据处理的问题,尤其是

Node.js net模块的使用示例

《Node.jsnet模块的使用示例》本文主要介绍了Node.jsnet模块的使用示例,net模块支持TCP通信,处理TCP连接和数据传输,具有一定的参考价值,感兴趣的可以了解一下... 目录简介引入 net 模块核心概念TCP (传输控制协议)Socket服务器TCP 服务器创建基本服务器服务器配置选项服