How to copy file using line with% UserProfile% in C #

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 this will not work if I do not change% UserProfile% to my actual username, how can I fix this?

+3
source share
3 answers

Instead:

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

to try

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

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

+2
source
var s = @"C:\Users\%UserProfile%\AppData\Roaming\";
var s2 = Environment.ExpandEnvironmentVariables(s);

s2 has advanced data

+2
source

The variable %userprofile%includes the full path, up to the root of the disk.

C:\Windows\System32>echo %userprofile%
C:\Users\[myusername]
0
source

All Articles