3

I am trying to copy a file using this -

 private void button1_Click(object sender, EventArgs e)
    {
        Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
        if (File.Exists(@"C:\Users\%UserProfile%\AppData\Roaming\.minecraft\bin\minecraft.jar"))
            try
            {                  
                File.Copy(@"C:\Users\%UserProfile%\AppData\Roaming\.minecraft\bin\minecraft.jar", @"C:\Users\%UserProfile%\Documents\MinecraftBackup\minecraft.jar", true);
            }

and it won't work unless I change %UserProfile% to my actual username, how do i fix this?

3 Answers3

2

Instead of:

C:\Users\%UserProfile%\AppData\Roaming\.minecraft\bin\minecraft.jar

try

Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), 
             @".minecraft\bin\minecraft.jar")

In fact, any time you see "C:\Users\%UserProfile%\AppData\Roaming\", you should use "Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)".

Steve Danner
  • 21,818
  • 7
  • 41
  • 51
2
var s = @"C:\Users\%UserProfile%\AppData\Roaming\";
var s2 = Environment.ExpandEnvironmentVariables(s);

s2 has expanded data

galets
  • 17,802
  • 19
  • 72
  • 101
0

The %userprofile% variable includes the full path, all the way back to the root of the drive.

C:\Windows\System32>echo %userprofile%
C:\Users\[myusername]
akatakritos
  • 9,836
  • 1
  • 23
  • 29
  • This is going to be used on other peoples computers so I dont believe that will work –  May 07 '12 at 19:56