3

我正在尝试使用此复制文件-

 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);
            }

除非我将 %UserProfile% 更改为我的实际用户名,否则它将无法工作,我该如何解决这个问题?

4

3 回答 3

2

代替:

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

尝试

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

事实上,任何时候你看到“ C:\Users\%UserProfile%\AppData\Roaming\”,你都应该使用“ Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)”。

于 2012-05-07T19:49:25.547 回答
2
var s = @"C:\Users\%UserProfile%\AppData\Roaming\";
var s2 = Environment.ExpandEnvironmentVariables(s);

s2有扩展数据

于 2012-05-07T20:02:25.867 回答
0

%userprofile%变量包括完整路径,一直到驱动器的根目录。

C:\Windows\System32>echo %userprofile%
C:\Users\[myusername]
于 2012-05-07T19:50:10.443 回答