0

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);
Tomas Walek
  • 2,516
  • 2
  • 23
  • 37
  • try the same arguments in the cmd shell and see if it succeeds b4 trying it from code. – Zenwalker Apr 20 '12 at 05:58
  • 1
    `xcopy` is a native program, not a shell built-in. You don't need to run it through `cmd`. – Joey Apr 20 '12 at 06:02
  • do you really need to use the command line? there are already several builtin classes which can do this job, too. using the command shell is not very easy if you want to handle possible exceptions/failors (especially evaluation of a problem beneath the returning result). See System.IO namespace and FileInfo/File classes at http://msdn.microsoft.com/de-de/library./system.io%28v=vs.90%29.aspx – Beachwalker Apr 20 '12 at 06:18
  • Why do you want (or need) to put an image to C:\windows\system32? This is not a good/common practice... you should (re)think about your software requirements and architecture. – Beachwalker Apr 20 '12 at 06:22

3 Answers3

1

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

Royi Namir
  • 144,742
  • 138
  • 468
  • 792
  • sir but if we wanna make a application in which user browse his own picture & then application copy it to c: drive then how would it take permission? – Jason Stathom Apr 20 '12 at 06:06
1

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.

Hailei
  • 42,163
  • 6
  • 44
  • 69
0

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.

Community
  • 1
  • 1
Waqar
  • 2,511
  • 18
  • 15