سەفین محمد
سەفین محمد

Reputation: 7

Copy file by c# access denied

I want to copy my application to D:\\Games using this code:

string path = "D:\\Games";
System.IO.File.Copy(path, Application.ExecutablePath, true);

but it gives me this error

Access to the path 'D:\Games' is denied.

it seams that it haven't access to this path.
How can I fix this ?

Upvotes: 0

Views: 2493

Answers (2)

el merzougui adil
el merzougui adil

Reputation: 1

Find the main executable of the program (bin\Debug) you want to run. Right-click or press and hold on it to open the contextual menu. Then, click or tap on Properties.

In the Properties window, go to the Compatibility tab. At the bottom of the window, check the box next to the “Run this program as an administrator” option, and then click or tap on Apply or OK.

Upvotes: 0

ian
ian

Reputation: 19

First thing you need to do is switch the source/target around. Source comes firs.

System.IO.File.Copy(Application.ExecutablePath, path, true);

Second problem is that string path needs to include the target filename. can't simply copy to a folder without giving the target file name.

Upvotes: 1

Related Questions