title: How to programmatically add x509 certificate to local machine store using c#
body: <p>I understand the question title may be a duplicate but I have not found an answer for my situation yet so here goes;</p>
  
  <p>I have this simple peice of code</p>
  
  <pre><code>// Convert the Filename to an X509 Certificate
  X509Certificate2 cert = new X509Certificate2(certificateFilePath);
  
  // Get the server certificate store
  X509Store store = new X509Store(StoreName.TrustedPeople, StoreLocation.LocalMachine);
  
  store.Open(OpenFlags.MaxAllowed);
  store.Add(cert); // x509 certificate created from a user supplied filename
  </code></pre>
  
  <p>But keep being presented with an "Access Denied" exception.</p>
  
  <p>I have read some information that suggests using StorePermissions would solve my issue but I don't think this is relevant in my code.
  Having said that, I did test it to to be sure and I couldn't get it to work.</p>
  
  <p>I also found suggestions that changing folder permissions within Windows was the way to go and while this may work(not tested), it doesn't seem practical for what will become distributed code.</p>
  
  <p>I also have to add that as the code will be running as a service on a server, adding the certificates to the current user store also seems wrong.</p>
  
  <p>Is there anyway to programmatically add a certificate into the local machine store?</p>