简体   繁体   English

C# 拒绝访问路径

[英]C# Access denied to path

string path = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86);
File.WriteAllBytes(path, Properties.Resources.chargementvideo); 

chargementvideo is a file in the resources of Vs. chargementvideo是 Vs 资源中的一个文件。

So the access gets denied to 'ProgramFiles' and I've already tried to run as admin or to give full permissions.因此,“ProgramFiles”的访问被拒绝,我已经尝试以管理员身份运行或授予完全权限。

When using File.WriteAllBytes , uou must provide the path to a file as first argument, not to a folder... otherwise the method cannot know to which file data must be written to:使用File.WriteAllBytes 时,您必须提供文件路径作为第一个参数,而不是文件夹...否则该方法无法知道必须将数据写入哪个文件:

String file = 'data.ext';
String path = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86);
String filePath = Path.Combine(path, file);

File.WriteAllBytes(filePath, Properties.Resources.chargementvideo);

Altrough I suggest you to avoid writing data into those folders... when accessing them, an elevation of privileges is necessary.虽然我建议您避免将数据写入这些文件夹......访问它们时,需要提升权限。 You cannot do it programmatically, but you can make your application run with Administrative Rights by editing its manifest as follows:您不能以编程方式执行此操作,但您可以通过如下编辑其清单来使您的应用程序以管理权限运行:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
  <v3:trustInfo xmlns:v3="urn:schemas-microsoft-com:asm.v3">
    <v3:security>
      <v3:requestedPrivileges>
        <v3:requestedExecutionLevel level="highestAvailable"/>
      </v3:requestedPrivileges>
    </v3:security>
  </v3:trustInfo>
</assembly>

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

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