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

chargementvideo is a file in the resources of Vs.

So the access gets denied to 'ProgramFiles' and I've already tried to run as admin or to give full permissions.

Enigmativity
  • 113,464
  • 11
  • 89
  • 172
  • Please provide links to the other solutions and explain why they don't apply to your situation. – Enigmativity Feb 25 '18 at 22:58
  • are you running in an elevated process – Daniel A. White Feb 25 '18 at 22:59
  • 1
    You're trying to write to a *directory*. You need to append a file name to the path. Or at least that's part of your problem. You still may not have permissions to write directly into program files. – pinkfloydx33 Feb 25 '18 at 22:59
  • Possible duplicate of [Why is access to the path denied?](https://stackoverflow.com/questions/8821410/why-is-access-to-the-path-denied) – jdphenix Feb 25 '18 at 22:59
  • You know you haven't actually asked us a question? – Enigmativity Feb 25 '18 at 23:00
  • It's already been stated that you're giving it the path to a folder. But just in case you put a filename on the end of that path.... "Program Files" is not somewhere that you as a programmer should be storing things. It will be secured against most users. Consider using an Application Data location, or why not just use the Temporary folder. (Path.GetTempPath()). – Richardissimo Feb 25 '18 at 23:08
  • The path could be changed to Desktop or an other, that was just for example – Alexandre Gerez Feb 25 '18 at 23:10

1 Answers1

3

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:

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>
Tommaso Belluzzo
  • 23,232
  • 8
  • 74
  • 98
  • Hello, thank you for your answer, the thing is that i want to create a new file that is the chargementvideo from the resources, and not to edit a new one – Alexandre Gerez Feb 25 '18 at 23:05
  • No it didnt ^^ but i think we had a missunderstanding on what i'm trying to do but thank you for your help :) – Alexandre Gerez Feb 25 '18 at 23:07
  • Great, what is the current problem (besides trying to write bytes into a folder instead of a file)? – Tommaso Belluzzo Feb 25 '18 at 23:08
  • The access is still denied even whil trying to edit the "data" file, same error – Alexandre Gerez Feb 25 '18 at 23:09
  • Repeating my comment from above... It's already been stated that you're giving it the path to a folder. But just in case you put a filename on the end of that path.... "Program Files" is not somewhere that you as a programmer should be storing things. It will be secured against most users. Consider using an Application Data location, or why not just use the Temporary folder. (Path.GetTempPath()). – Richardissimo Feb 25 '18 at 23:09
  • Actually, you should never write temporary files into that folder... but if you REALLY want to... you have to make your application run with Administrative Rights. You can't do it within your code, but you can request to elevate the rights of the assembly while it's running: https://en.wikipedia.org/wiki/User_Account_Control#Requesting_elevation – Tommaso Belluzzo Feb 25 '18 at 23:12