Visual Basic Code , VB.NET Code, VB Code
  Home   :  Code   :  Forums   :  Submit   :  Mailing List   :  About   :  Contact


Current User Location Error


Current User Location Error

Author
Message
allgames71
allgames71
Forum God
Forum God (2.3K reputation)Forum God (2.3K reputation)Forum God (2.3K reputation)Forum God (2.3K reputation)Forum God (2.3K reputation)Forum God (2.3K reputation)Forum God (2.3K reputation)Forum God (2.3K reputation)Forum God (2.3K reputation)

Group: Forum Members
Posts: 6, Visits: 7
Hi ,

I write to get the current user name , and the result is :

A> In line 15 , the result is "Raymond\Favorites" , where Raymond is the current user name which locate at Drive D:\

     My question is : how can I get "D:\Raymond\Favorites" ???

B> In line 16 , the result is "Raymond\Documents" in Win7

     My question is : why is it not "Raymond\Favorites"

     I've try to use : FileName = sUserName & "\Videos" , but with the same result : "Raymond\Documents" ???

1. Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" _
2.                             (ByVal lpBuffer As String, nSize As Long) As Long

3. Private Sub Form_Load()
4. Dim sUserName As String
5. Dim lSize As Long

6. lSize = 20
7. sUserName = Space(20)
8. Call GetUserName(sUserName, lSize)
9. sUserName = Trim(sUserName)
10. sUserName = Left(sUserName, Len(sUserName) - 1)
11. '------------------------------------------------------------------------------
12. Dim Res
13. Dim FileName
14. FileName = sUserName & "\Favorites"
15. MsgBox FileName
16. Res = Shell("explorer.exe " & FileName, vbNormalFocus)

17. End Sub

Thanks for everybody.

Mark
Mark
Forum God
Forum God (139K reputation)

Group: Moderators
Posts: 1.1K, Visits: 11K
allgames71 (10/12/2012)

My question is : how can I get "D:\Raymond\Favorites" ???

If you know the drive letter ahead of time then changing line 14 to

FileName = "D:\" & sUserName & "\Favorites"

If you need to search the drives to find which drive has the folder then I would use the FileSystemObject and loop through the Drives collection and use the FolderExists function to see if the drive has the folder you are lookind for. Something like this should work. (make sure you set a reference to the "Microsoft Scripting Runtime"

Private Sub Command1_Click()
Dim fso As FileSystemObject
Dim drv As Drive
Dim strDir As String
Dim strSearch As String


    strDir = "\Software\WinZip"

    Set fso = New FileSystemObject
    
    For Each drv In fso.Drives
        If drv.IsReady Then
            strSearch = drv.Path & strDir
            If fso.FolderExists(strSearch) Then
                strDir = drv.Path & strDir
                Exit For
            End If
        End If
    Next
    
    MsgBox strDir
    Set fso = Nothing
End Sub


 

 

 

allgames71 (10/12/2012)

In line 16 , the result is "Raymond\Documents" in Win7

I assume this means when explorer opens the location it opens to is Raymond\Documents. If so that means it could not locate Raymond\Favorites so when explorer is shelled it is opening to it's default location.

allgames71
allgames71
Forum God
Forum God (2.3K reputation)Forum God (2.3K reputation)Forum God (2.3K reputation)Forum God (2.3K reputation)Forum God (2.3K reputation)Forum God (2.3K reputation)Forum God (2.3K reputation)Forum God (2.3K reputation)Forum God (2.3K reputation)

Group: Forum Members
Posts: 6, Visits: 7
Thanks for your reply ,

It's very strange that in your program , line 2

"Dim fso As FileSystemObject"

When I run the program , there's an error saying the "fso" is not defined.

I encounter this before , I'm using VB6. Do I have to add something to make this "fso" work.

As my VB6 knowledge is not good , would you mind to tell me how.

Thanks

Mark
Mark
Forum God
Forum God (139K reputation)

Group: Moderators
Posts: 1.1K, Visits: 11K
That is why I added the comment to make sure you set a reference to the Microsoft Scripting Runtime.
allgames71
allgames71
Forum God
Forum God (2.3K reputation)Forum God (2.3K reputation)Forum God (2.3K reputation)Forum God (2.3K reputation)Forum God (2.3K reputation)Forum God (2.3K reputation)Forum God (2.3K reputation)Forum God (2.3K reputation)Forum God (2.3K reputation)

Group: Forum Members
Posts: 6, Visits: 7
Thanks for your reply ,

As my VB6 knowledge is poor , I just know how to add "Microsoft Scripting Runtime"

Thanks for your tutor.

Mark
Mark
Forum God
Forum God (139K reputation)

Group: Moderators
Posts: 1.1K, Visits: 11K
With your project open, click on the Project menu. Scroll down towards the bottom and select References from the menu. From the dialog the opens, place a check mark next to "Micorsoft Scripting Runtime" in the list and then click the OK button.
GO


Similar Topics


Reading This Topic


Login
Existing Account
Email Address:


Password:


Social Logins

Select a Forum....

















A1VBCode Forums


Search