I am making a program that I want to load music from library/music on any computer. To do this I use Music.FromUri(). However, I need the username from the current computer in order for the filepath to work.

Is there any way to get the username and use it in the filepath?

  • 2
    \$\begingroup\$ If you're planning on constructing a path like C:\Users\Foo\Music, don't. That assumes that the Users directory is named like that, it assumes that the user profile lives in that directory, it assumes that Music is not redirected, it assumes that there in fact exists a C: drive, and that that's where Users are at. If you want a particular Known Folder, use the managed flavors of SHGetFolderPath or SHGetKnownFolderPath to properly retrieve it. \$\endgroup\$ – Lars Viklund May 13 '12 at 10:26
  • \$\begingroup\$ and how do i use those? \$\endgroup\$ – Eric Nilsson May 13 '12 at 11:00
  • \$\begingroup\$ Wouldn't it be better to let the user choose where to load the music from manually? Personally I've never put any music in the Library. \$\endgroup\$ – David Gouveia May 13 '12 at 11:03
  • \$\begingroup\$ Well, if i use Music.FromUri it reads in all subfiles from a file so there will be very many files to chose from. Is there any way to make the FromUri class just read a file and not subfiles? \$\endgroup\$ – Eric Nilsson May 13 '12 at 11:12
  • \$\begingroup\$ What's a subfile? \$\endgroup\$ – David Gouveia May 13 '12 at 11:13

You can get the current username with:


But if you were going to use it to construct a path to the Music folder, there's an easier way:


Here's a list of all the folders you can get this way.

| improve this answer | |

You used the XNA tag, so this should give you what you want: Environment.UserName. Here's the doc.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.