dcsimg
Results 1 to 5 of 5

Thread: [RESOLVED] Get username from user folder

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2018
    Location
    Slovakia
    Posts
    79

    Resolved [RESOLVED] Get username from user folder

    Hi, I have problems when Iīm trying to get username path via Evironment variable.

    Code:
    Dim usernamepath As String
    
    usernamepath = Environ("USERPROFILE")
    "C:\users\" + usernamepath + "desktop\" + nazovsuboru
    But that code does not work. My code performs well without any errors, but resulting file isnīt created at all even Iīm able to see "Done!" msgbox.

    Thanks.

  2. #2
    Frenzied Member PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Newport, UK
    Posts
    1,078

    Re: Get username from user folder

    The userprofile variable is the path to the user profile, you don't need to concatenate anything. Also if you are using .Net you can just use https://docs.microsoft.com/en-us/dot...tframework-4.8 anyway.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Oct 2018
    Location
    Slovakia
    Posts
    79

    Re: Get username from user folder

    Thanks. Iīve tried this code, but it does not work either.

    Code:
    Dim usernamepath As String = Environ("USERPROFILE")
    Dim usernamepath2 As String = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)
    usernamepath2 + "desktop\" + nazovsuboru
    btw, last line should looks like that - C:\users\acer\desktop\myfile.bat.exe

    Note that Iīve tried both username strings individually.

    I tried to show usernamepath2 via msgbox and Iīm able to see the correct path. But file isnīt created at all. Maybe the external software isnīt able to handle this situation? I donīt think so, but I think that this could be resolved via getting the path directly to textbox and concatenate it with other strings that I have prepared. But, is there simplifier way?
    Last edited by VB.NET Developer; Aug 21st, 2019 at 12:59 PM. Reason: small typo in code

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Oct 2018
    Location
    Slovakia
    Posts
    79

    Re: Get username from user folder

    I resolved it! Problem was in the missing slash after usernamepath (marked in red), but I didnīt noticed it at msgbox.
    Code:
    Dim usernamepath As String = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)
    usernamepath + "\desktop\" + nazovsuboru
    Thanks a lot!
    Last edited by VB.NET Developer; Aug 21st, 2019 at 01:11 PM.

  5. #5
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    5,333

    Re: [RESOLVED] Get username from user folder

    I was wondering, and I think PlausiblyDamp was probably suggesting as well, that if you wanted to get to the Desktop of the user, why didn't just use the SpecialFolder Desktop enumeration and save yourself a concatenation.
    Also, you should use the string concatenation operator & instead of + to be on the safe side. Sometimes + will convert strings if they are convertible to numbers, to numbers and add them rather than concatenate them, so you may not get what you intended.

    But, if you're building a path, then there are methods that will build a path from strings, so that is probably preferable anyway.
    Code:
    IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), nazovsuboru)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width