简体   繁体   中英

how to copy a file from any directory to c drive using cmd in c#

i tried an image file to copy in c:(operating sys) drive,but it says error wid access denied,i have used as

string strCmdLine;
    strCmdLine = @" /c xcopy d:\123.png C:\windows\system32";
    Process.Start("CMD.exe", strCmdLine);

you probably dont have enough permissions....

try adding credentials :

    Process p = new Process();
    process.StartInfo.UserName = "aaaa";  
    process.StartInfo.Password = "xxxxx";
...
...

also , verify that :

read permissions for : d:\\123.png

write permissions for : C:\\windows\\system32

Access Denied may be caused by several reasons, such as user permissions or file being in use. Since the command line seems to be OK, I suggest to check whether your application was run by a Windows user that has permission to write to C:\\windows\\system32.

you need to run CMD.exe as admin try following

        Process p = new Process();
        p.StartInfo.FileName = "cmd.exe";
        p.StartInfo.Verb = "runas";
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.RedirectStandardInput = true;
        p.Start();

        p.StandardInput.WriteLine(@"/c xcopy d:\123.png C:\windows\system32");

You can check This post which shows how to run program as admin.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM