答案 0 :(得分:2)
尝试:
File.Copy(@"c:\Program Files(86)\Program Folder\mydll.dll", @"C:\mydll.dll");
要强制覆盖,只需使用:
File.Copy(@"c:\Program Files(86)\Program Folder\mydll.dll", @"C:\mydll.dll",true);
根据{{3}}:
public static void Copy(
string sourceFileName,
string destFileName,
bool overwrite
)
参数
sourceFileName类型:System.String要复制的文件。
destFileName类型:System.String目标文件的名称。 这不能是目录。
overwrite类型:System.Boolean如果目标文件可以,则为true 覆盖;否则,错误。
如另一个答案中所述,您可能没有适当的权限,可以尝试通过以下方式进行检查:
string directoryPath = @"c:\Program Files(86)\Program Folder";
bool isWriteAccess = false;
try
{
AuthorizationRuleCollection collection = Directory.GetAccessControl(directoryPath).GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount));
foreach (FileSystemAccessRule rule in collection)
{
if (rule.AccessControlType == AccessControlType.Allow)
{
isWriteAccess = true;
break;
}
}
}
catch (UnauthorizedAccessException ex)
{
isWriteAccess = false;
}
catch (Exception ex)
{
isWriteAccess = false;
}
if (!isWriteAccess)//we cant write to location
{
//handle notifications
}else {//we can write to the location
}
答案 1 :(得分:1)