简体   繁体   English

Windows 7和我的C#程序中的复制问题

[英]Problem with copy in Windows 7 and my C# program

I do this in my C# program: 我在C#程序中这样做:

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. 它仅在Windows 7中才会发生,在Windows XP中它的运行效果非常好。

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)). 默认情况下,Windows 7中的用户帐户是受限的(非管理员),因此您的程序不能只写系统上的任何地方(这就是好东西(TM))。 Try putting Test.txt in another directory, for example the temp directory (ask the system where that is). 尝试将Test.txt放在另一个目录中,例如temp目录(询问系统所在的目录)。

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

In addition to what others said try using Special Folders . 除了其他人所说的之外,请尝试使用特殊文件夹 and learn a little bit about Making Your Application UAC Aware 并了解有关使您的应用程序具有UAC意识的知识

Windows 7 don't allow to access the program folders and the root folder. Windows 7 不允许访问程序文件夹和根文件夹。 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(); 您可以使用Environment.GetFolderPath();定位此路径。

Example

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

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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