Take the 2-minute tour ×

This question already has an answer here:

in the code below I'm trying to extract a dll file from the running application and save it to System32 directory :

  using System;
  using System.Collections.Generic;
  using System.Text;
  using System.IO;
  using System.Diagnostics;
  using Microsoft.VisualBasic;
  using System.Security.Cryptography;
  using System.Runtime.InteropServices;
  using System.Reflection;
  using System.Windows.Forms;

   namespace ConsoleApplication1
class Program

    public static void ExtractSaveResource(String filename, String location)
        //  Assembly assembly = Assembly.GetExecutingAssembly();
        Assembly a = Assembly.GetExecutingAssembly();
        // Stream stream = assembly.GetManifestResourceStream("Installer.Properties.mydll.dll"); // or whatever 
        // string my_namespace = a.GetName().Name.ToString();
        Stream resFilestream = a.GetManifestResourceStream(filename);
        if (resFilestream != null)
                BinaryReader br = new BinaryReader(resFilestream);
                FileStream fs = new FileStream(location, FileMode.Create); // say 
                BinaryWriter bw = new BinaryWriter(fs);
                byte[] ba = new byte[resFilestream.Length];
                resFilestream.Read(ba, 0, ba.Length);
            catch (Exception E) { MessageBox.Show(E.Message); }
        // this.Close(); 


    static void Main(string[] args)
        string systemDir = Environment.SystemDirectory;


Exception Message :

Access to the path C:\Windows\System32 is denied

I tried to copy the file into other directory like D:\ or X:\ but i always get this exception message
How could this be solved ?

share|improve this question

marked as duplicate by Bill the Lizard Aug 21 at 15:16


You're writing to C:\Windows\System32 because you use Environment.SystemDirectory explicitly; if you were writing to `D:` I don't think you'd see this problem. Are you running elevated? Remember that in Windows there is difference between an "Administrator user" and "Running as adminstrator." –  Dan Puzey Oct 23 '12 at 15:37

1 Answer 1

You're using Environment.SystemDirectory, which is why it's trying to save out there. However, even if you're running under the Administrator user, certain system folders are protected. The system32 folder is one of them. You would have to turn off UAC temporarily to save something there programmatically, which is not really a good thing to do.

I would very strongly recommend that you find a different/better way to do this.

share|improve this answer
I tried to save the file into another non-protected directory like D:\\ but i get an access denied error ?? –  Rafik Bari Oct 23 '12 at 15:45
@ShikataGaNai Try Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);. This will save to the current user's My Documents folder, where you're guaranteed to have write access. if you still get an access denied error there you're dealing with something other than the obvious. –  YYY Oct 23 '12 at 15:48
The access to MyDocuments is also denied ? How could this be ! –  Rafik Bari Oct 23 '12 at 15:54
@ShikataGaNai Try changing your stream invocation to new FileStream(location, FileMode.Create, FileIOPermissionAccess.Write);. I have a sneaking suspicion you never requested write permissions. Alternatively, or additionally, check what FileStream.CanWrite equals. –  YYY Oct 23 '12 at 16:01
