使用c#中的kernel32.dll缩短长文件路径名



我正在尝试将长文件路径转换为短文件路径。尝试使用下面提到的kernel32.dll中可用的GetShortPathName的所有签名。但所有文件都返回一个空字符串,而不是一个短文件路径。在win 10中的64位机器上使用c#windows应用程序(.net框架4.6.1(进行测试。请告诉我如何获取短文件路径。下面是我的代码。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsFormsApp1
{
public partial class Form2 : Form
{
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern uint GetShortPathName(
[MarshalAs(UnmanagedType.LPTStr)]
string lpszLongPath,
[MarshalAs(UnmanagedType.LPTStr)]
StringBuilder lpszShortPath,
uint cchBuffer);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern uint GetShortPathName(string lpszLongPath, char[] lpszShortPath, int cchBuffer);

[DllImport("kernel32.dll", CharSet = CharSet.Unicode, EntryPoint = "GetShortPathNameW", 
SetLastError = true)]
static extern int GetShortPathName(string pathName, System.Text.StringBuilder shortName, int 
cbShortName);
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
string longFilePath = @"C:EAuthors filesGOLDddd Retail Leasesddd Owned or Co-Owned Centres (including Property Income Fund Properties) – Managed by dddGarden City Booragoon (WA)Specialty Leases & DeedsShop 12A35120-#5337137, v1 _VICProduction1_ - Shop xxx - Garden City, Booragoon - D.C.K Australia Pty Ltd t_as Lovisa - Executed Deed of Assignment and Variation of Lease and Consent to Assignment   - WorkSite Acrobat Integration.pdf";

string s1 = ShortPath(longFilePath);
string s2=ToShortPathName(longFilePath);            
string s3 = GetShortPathName(longFilePath);
}
public static string ToShortPathName(string longName)
{
uint bufferSize = 256;           
StringBuilder shortNameBuffer = new StringBuilder((int)bufferSize);
GetShortPathName(longName, shortNameBuffer, bufferSize);
return shortNameBuffer.ToString();
}
public string ShortPath(string longpath)
{
char[] buffer = new char[256];
GetShortPathName(longpath, buffer, buffer.Length);
return new string(buffer);
}
public static string GetShortPathName(string longFileName)
{           
uint bufferSize = 256;                       
StringBuilder shortNameBuffer = new StringBuilder((int)bufferSize);
uint result = GetShortPathName(longFileName, shortNameBuffer, bufferSize);
return shortNameBuffer.ToString();
}
}
}

也许太晚了,但诀窍是将"\?"预先发送到路径

const int MAX_PATH = 255;
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, EntryPoint = "GetShortPathNameW", SetLastError = true)]
static extern int GetShortPathName(
[MarshalAs(UnmanagedType.LPTStr)]
string pathName,
[MarshalAs(UnmanagedType.LPTStr)]
StringBuilder shortName, int cbShortName);
public static string GetShortPath(string path)
{
var shortPath = new StringBuilder(MAX_PATH);
GetShortPathName($"\\?\{path}", shortPath, MAX_PATH);
var shortenedPath = shortPath.ToString();
return shortenedPath.Replace("\\?\", string.Empty);
}
var longPath = @"C:EAuthors filesGOLDddd Retail Leasesddd Owned or Co-Owned Centres (including Property Income Fund Properties) – Managed by dddGarden City Booragoon (WA)Specialty Leases & DeedsShop 12A35120-#5337137, v1 _VICProduction1_ - Shop xxx - Garden City, Booragoon - D.C.K Australia Pty Ltd t_as Lovisa - Executed Deed of Assignment and Variation of Lease and Consent to Assignment   - WorkSite Acrobat Integration.pdf";
var shortenedPath = GetShortPath(longPath);

资源:

最大路径长度限制

最新更新