我需要允许用户选择一个图标,因此我已经从shell32实现了PickIconDlg函数。问题在于,如果用户选择的路径长于我声明的初始路径,则结果值是用户选择的路径,其长度将被截断为初始路径的长度。
例如,如果我将初始路径设置为“ C:\ Windows \ System32 \ shell32.dll”,并且用户选择“ C:\ Users \ Public \ Documents \ TheIcons \ Library.dll”,则更新后的字符串值返回为“ C:\ Users \ Public \ Documents \ TheIc”(即用户选择的路径的前31个字符,因为初始路径的长度为31个字符)。
我尝试调整传递给PickIconDlg的'nMaxFile'值,据我了解,该值应该设置路径变量的最大长度。这似乎没有什么不同。
Declare Unicode Function PickIconDlg Lib "Shell32" Alias "PickIconDlg" (ByVal hwndOwner As IntPtr, ByVal lpstrFile As String, ByVal nMaxFile As Integer, ByRef lpdwIconIndex As Integer) As Integer
Public Function GetIconLoc(frmform As Form) As Object()
Dim iconfile As String = Environment.GetFolderPath(Environment.SpecialFolder.System) + "\shell32.dll"
Dim iconindex As Integer ' Will store the index of the selected icon
If PickIconDlg(frmform.Handle, iconfile, 50, iconindex) = 1 Then
MessageBox.Show(iconfile)
Return {iconfile, iconindex}
Else
Return Nothing
End If
End Function
我希望字符串变量iconfile包含完整的用户选择的路径,因为它的长度小于50个字符的定义最大值。而是如上所述,仅返回路径的一部分。
答案 0 :(得分:0)
在原始文件名后添加一个Null字符和空白,以创建一个足以容纳结果的字符串缓冲区。
Dim iconfile As String = _
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "shell32.dll") & _
vbNullChar & Space(256)
然后在调用函数时传递总长度
PickIconDlg(frmform.Handle, iconfile, Len(iconfile), iconindex)
最后从结果中减少多余的空间
iconfile = Left(iconfile, InStr(iconfile, vbNullChar) - 1)
此外,请使用Path.Combine
而不是自己串联路径。 Path.Combine
自动添加缺失字符或删除多余的反斜杠(\
)。