简体   繁体   中英

Problem with copy in Windows 7 and my C# program

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.

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).

Win 7阻止系统驱动器上的根文件夹...将文件放在您有权复制的位置。

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

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"

最好使用Path.Join加入文件和路径

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

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