Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
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. – Lars Viklund May 13 '12 at 10:26
and how do i use those? – Eric Nilsson May 13 '12 at 11:00
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. – David Gouveia May 13 '12 at 11:03
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? – Eric Nilsson May 13 '12 at 11:12
What's a subfile? – 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.

share|improve this answer

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

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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