微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

GetShortPathNameA不能在C#中工作

我试图从一个文件名获得短文件名,但我有问题的C#代码。 VB.Net代码是:

Declare Function GetShortPathName Lib "kernel32" _ Alias "GetShortPathNameA" (ByVal lpszLongPath As String,_ ByVal lpszShortPath As String,ByVal cchBuffer As Long) As Long Public Function GetShortName(ByVal sLongFileName As String) As String Dim lRetVal As Long,sShortPathName As String,iLen As Integer 'Set up buffer area for API function call return sShortPathName = Space(255) iLen = Len(sShortPathName) 'Call the function lRetVal = GetShortPathName(sLongFileName,sShortPathName,iLen) 'Strip away unwanted characters. GetShortName = Left(sShortPathName,lRetVal) End Function

我已经将这个函数转换为c#:

[DllImport("kernel32",EntryPoint = "GetShortPathNameA")] static extern long GetShortPathName(string lpszLongPath,string lpszShortPath,long cchBuffer); public string GetShortName(string sLongFileName) { long lRetVal; string sShortPathName; int iLen; // Set up buffer area for API function call return sShortPathName = new String(' ',1024); iLen = sShortPathName.Length; // Call the function lRetVal = GetShortPathName(sLongFileName,iLen); // Strip away unwanted characters. return sShortPathName.Trim(); }

但我不能得到C#版本的工作。 我错过了什么或错在哪里?

如何创build从C#到C + +委托,将IEnumerable作为参数与SWIG的通过?

在启动时dynamic决定Winforms或Wpf?

Windows证书存储

wasabi阻止Azure缩放收集数据的频率如何?

我可以在.NET中获得打印机的图像吗?

了解vs2010的基本Windows编程

在.NET中的任务栏中右键单击应用程序菜单

将.NETconfiguration为在Windows 8上使用3.5,在Windows 8上使用4.5

.NET与Windows RT蓝牙LE API – 重新启动Windows后无法读取或写入设备

我可以限制进程的RAM使用情况吗?

VB声明的历史可以追溯到VB6,对于.NET语言来说是相当不恰当的。 尽管P / Invoke编组将允许非托管代码涂写到一个字符串中,但由于字符串interning而导致随机失败。 你也真的想使用Unicode版本,所以你不会得到意想不到的字符翻译。 如果函数失败,你想做一些有意义的事情。 这是我的版本:

public static string GetShortName(string sLongFileName) { var buffer = new StringBuilder(259); int len = GetShortPathName(sLongFileName,buffer,buffer.Capacity); if (len == 0) throw new System.ComponentModel.Win32Exception(); return buffer.ToString(); } [DllImport("kernel32",EntryPoint = "GetShortPathName",CharSet = CharSet.Auto,SetLastError = true)] private static extern int GetShortPathName(string longPath,StringBuilder shortPath,int bufSize);

也许是因为CharSet / Marshaling,还要记住字符串是不可变的。 尝试以下操作:

[DllImport("kernel32.dll",SetLastError=true)] static extern uint GetShortPathName( [MarshalAs(UnmanagedType.LPTStr)] string lpszLongPath,[MarshalAs(UnmanagedType.LPTStr)] StringBuilder lpszShortPath,uint cchBuffer);

(从pinvoke )

/// <summary> /// The ToLongPathNametoShortPathName function retrieves the short path form of a specified long input path /// </summary> /// <param name="longName">The long name path</param> /// <returns>A short name path string</returns> public static string ToShortPathName(string longName) { uint bufferSize = 256; // don´t allocate stringbuilder here but outside of the function for fast access StringBuilder shortNameBuffer = new StringBuilder((int)bufferSize); uint result = GetShortPathName(longName,shortNameBuffer,bufferSize); return shortNameBuffer.ToString(); }

pinvoke.net列出在这样的导入:

[DllImport("kernel32.dll",uint cchBuffer);

pinvoke.net是一个伟大的参考,这些类型的问题,你不完全确定如何翻译的特点。

这会改变你的代码,如下所示:

public static string GetShortName(string sLongFileName) { long lRetVal; int iLen = 1024; StringBuilder sShortPathName = new StringBuilder(iLen); // Call the function lRetVal = GetShortPathName(sLongFileName,iLen); // Strip away unwanted characters. return sShortPathName.ToString(); }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐