Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SHGetFolderLocation API

Status
Not open for further replies.

Promis

IS-IT--Management
Feb 19, 2003
6
0
0
US
Anyone successfully used this in VB.Net. I've been trying, unsuccessfuly however.

I been using the following:

Public Enum eCSIDL
CSIDL_DESKTOP = 0
CSIDL_INTERNET = &H1
CSIDL_PROGRAMS = &H2
CSIDL_CONTROLS = &H3
CSIDL_PRINTERS = &H4
CSIDL_PERSONAL = &H5
CSIDL_FAVORITES = &H6
CSIDL_STARTUP = &H7
End Enum

Public Const SHGFP_TYPE_CURRENT = &H0
Public Const SHGFP_TYPE_DEFAULT = &H1
Public Const MAX_PATH = 260
Public Const S_OK = 0
Public Const S_FALSE = 1

<DllImport(&quot;SHFOLDER.DLL&quot;)> _
Public Function _
SHGetFolderPath( _
ByVal hwndOwner As IntPtr, _
ByVal nFolder As eCSIDL, _
ByVal hToken As Int32, _
ByVal dwReserved As Int32, _
ByVal lpszPath As String _
) As Int32
End Function

Private Function GetFolderPath(ByVal nCSIDL As eCSIDL) As String

Dim strPath As String = New String(&quot; &quot;c, 260)

Dim i As Integer

Try
If SHGetFolderPath(Me.Handle, nCSIDL, 0&, _ SHGFP_TYPE_CURRENT, strPath) = S_OK Then
GetFolderPath = Microsoft.VisualBasic.Left(strPath, InStr(strPath, Chr(0)) - 1)
End If
Catch ex As Exception
strPath = ex.Message
End Try

Return strPath

End Function

Dim sPath as String
sPath = GetFolderPath(Gobal.eCSIDL.CSIDL_PERSONAL)

Using CSIDL_PERSONAL should return the path to My Documents. So far all that is returned is an empty string.

I've tried changing the API Declare to use ByRef lpszPath As String instead of ByVal lpszPath As String but it doesn't seem to make any difference in the final result.

Anyone got any ideas on how to make this work?

Thanks,

Promis
 
Why are you calling out to the API, when you can do this?
Code:
MyDocPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal)

Be aware that your assembly will need FileIOPermission in the .NET Framework Security.

Chip H.
 
Thank you.

I was using the API because soemone had recommended is as the way to retrieve the path to My Documents.

I must admit however that Environment.GetFolderPath(......

is much easier and cleaner.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top