.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

相关文章

javax.net.ssl.SSLHandshakeException:异常原因及解决方案

《javax.net.ssl.SSLHandshakeException:异常原因及解决方案》javax.net.ssl.SSLHandshakeException是一个SSL握手异常,通常在建立SS... 目录报错原因在程序中绕过服务器的安全验证注意点最后多说一句报错原因一般出现这种问题是因为目标服务器

SQL Server修改数据库名及物理数据文件名操作步骤

《SQLServer修改数据库名及物理数据文件名操作步骤》在SQLServer中重命名数据库是一个常见的操作,但需要确保用户具有足够的权限来执行此操作,:本文主要介绍SQLServer修改数据... 目录一、背景介绍二、操作步骤2.1 设置为单用户模式(断开连接)2.2 修改数据库名称2.3 查找逻辑文件名

C#如何去掉文件夹或文件名非法字符

《C#如何去掉文件夹或文件名非法字符》:本文主要介绍C#如何去掉文件夹或文件名非法字符的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录C#去掉文件夹或文件名非法字符net类库提供了非法字符的数组这里还有个小窍门总结C#去掉文件夹或文件名非法字符实现有输入字

Java如何根据文件名前缀自动分组图片文件

《Java如何根据文件名前缀自动分组图片文件》一大堆文件(比如图片)堆在一个目录下,它们的命名规则遵循一定的格式,混在一起很难管理,所以本文小编就和大家介绍一下如何使用Java根据文件名前缀自动分组图... 目录需求背景分析思路实现代码输出结果知识扩展需求一大堆文件(比如图片)堆在一个目录下,它们的命名规

vscode不能打开终端问题的解决办法

《vscode不能打开终端问题的解决办法》:本文主要介绍vscode不能打开终端问题的解决办法,问题的根源是Windows的安全软件限制了PowerShell的运行,而VSCode默认使用Powe... 遇到vscode不能打开终端问题,一直以为是安全软件限制问题,也没搜到解决方案,因为影响也不大,就没有管

使用easy connect之后,maven无法使用,原来需要配置-Djava.net.preferIPv4Stack=true问题

《使用easyconnect之后,maven无法使用,原来需要配置-Djava.net.preferIPv4Stack=true问题》:本文主要介绍使用easyconnect之后,maven无法... 目录使用easGWowCy connect之后,maven无法使用,原来需要配置-DJava.net.pr

在.NET平台使用C#为PDF添加各种类型的表单域的方法

《在.NET平台使用C#为PDF添加各种类型的表单域的方法》在日常办公系统开发中,涉及PDF处理相关的开发时,生成可填写的PDF表单是一种常见需求,与静态PDF不同,带有**表单域的文档支持用户直接在... 目录引言使用 PdfTextBoxField 添加文本输入域使用 PdfComboBoxField

Spring Boot中JSON数值溢出问题从报错到优雅解决办法

《SpringBoot中JSON数值溢出问题从报错到优雅解决办法》:本文主要介绍SpringBoot中JSON数值溢出问题从报错到优雅的解决办法,通过修改字段类型为Long、添加全局异常处理和... 目录一、问题背景:为什么我的接口突然报错了?二、为什么会发生这个错误?1. Java 数据类型的“容量”限制

Python运行中频繁出现Restart提示的解决办法

《Python运行中频繁出现Restart提示的解决办法》在编程的世界里,遇到各种奇怪的问题是家常便饭,但是,当你的Python程序在运行过程中频繁出现“Restart”提示时,这可能不仅仅是令人头疼... 目录问题描述代码示例无限循环递归调用内存泄漏解决方案1. 检查代码逻辑无限循环递归调用内存泄漏2.

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

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