如何编码文件路径和URL
问题描述:
朋友您好,请在此问题上需要帮助.
我想转换文件路径.例如,文件路径为
Hello friends, please I need help concerning this issue.
I would like to convert file path. For example, a file path is
C:\Documents and Settings\Uncle Joe\My Document\manga.zip
我想将其转换为
and I would like to convert it to something like
C:\DOCUMEN~\Uncl~\MY DOC~\manga.zip
我也想知道如何编码URL.
例如:URL是
I also would like to know how I can encode an URL.
For example: an URL is
http://www.google.com/search.php
编码后应该变成
and after encoding it should become something like
http%3A%2F%2Fwww.google.com%2Fsearch.php
如果无法使用VB.NET,请给我另一种方法.
我需要完成这些事情.
Please if it is not possible using VB.NET kindly give me another means to do it.
I need to get these things done.
答
DOS 8.3路径和文件名需要kernel32函数GetShortPathName:
DOS 8.3 path and file names need the kernel32 funtion GetShortPathName:
Private 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
sShortPathName = Space(255)
iLen = Len(sShortPathName)
lRetVal = GetShortPathName(sLongFileName, sShortPathName, iLen)
GetShortName = Left(sShortPathName, lRetVal)
End Function
第一个示例显示了以下内容看起来像DOS短名称,因为它们是NTFS引入的,目的是与旧版软件兼容(然后此示例书写不正确:不允许使用空白字符).您是否需要与为MS-DOS或类似语言编写的代码兼容?最好一定要避免这种情况.坦率地说,我认为您真的不需要使用这些名称.
至于第二次转换,它看起来像URLEncode.使用方法System.Web.HttpServerUtility.UrlEncode
, http://msdn.microsoft.com/en-us/library/zttxte6w .aspx [^ ] .—SA
First example shows something which looks like DOS short names as they were introduced with introduction of NTFS for compatibility with legacy software (then this example is written incorrectly: blank character is not allowed). Do you need compatibility with code written for MS-DOS or something like that? Better avoid it by all means. I don''t think you really need to use such names, frankly.
As to the second conversion, it looks like URLEncode. Use the methodSystem.Web.HttpServerUtility.UrlEncode
, http://msdn.microsoft.com/en-us/library/zttxte6w.aspx[^].—SA