tags:

views:

458

answers:

5

I do this in my C# program:

File.copy(Application.StartupPath + "\Test.txt",@"c:\Test.txt");

But I get this error:

Access to  the path 'c\Test.txt' is denied

It happens only in Windows 7, in Windows XP it works excellent.

+2  A: 

Win 7 blocks root folder on system drive... put the file in a place you have permissions to copy.

Dani
+1  A: 

Windows 7 don't allow to access the program folders and the root folder. You can give the Directory writer access or change the destination path to one user Folder, like the "My Documents" or an App Directory.

You can loacate this paths with Environment.GetFolderPath();

Example

Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); retrurns: "C:\Users\Admin\AppData\Roaming"

Andreas Hoffmann
+9  A: 

Access is denied. That means you don't have access. No, really, it does.

User accounts in Windows 7 are limited (non-Administrator) by default, so your program cannot just write anywhere on the system (and that is a Good Thing (TM)). Try putting Test.txt in another directory, for example the temp directory (ask the system where that is).

Thomas
and how to force this copy in C# (is there any class that deal with) ?(if it can be doing)
Gold
If you could force it, that would make security privileges rather useless, wouldn't it? But you can request elevated privileges, if you must. The user will then be asked for approval, with the dark gray screen and all that. I have never done this, but read up on UAC if you really want to do this. (I wouldn't want any app to request this just for writing a Test.txt into the C: root!)
Thomas
+2  A: 

In addition to what others said try using Special Folders. and learn a little bit about Making Your Application UAC Aware

Shoban
+1  A: 

It's best to join a file & path with Path.Join

File.copy(Path.Join(Application.StartupPath, "\Test.txt"), @"c:\Test.txt");
Carra