Opc.Ua.WindowsCertificateStore.PermanentlyDeleteStore C# (CSharp) Method

PermanentlyDeleteStore() public method

Deletes the store and all certificates contained within it.
public PermanentlyDeleteStore ( ) : void
return void
        public void PermanentlyDeleteStore()
        {   
            lock (m_lock)
            {   
                // check for any certificates.
                X509Certificate2Collection certificates = Enumerate();

                if (certificates.Count > 0)
                {
                    throw ServiceResultException.Create(
                        StatusCodes.BadNotWritable,
                        "Cannot delete a store that contains certificates.\r\nType={0}, Name={1}", 
		                m_storeType,
		                m_symbolicName);
                }
                
	            IntPtr hStore = IntPtr.Zero;
	            IntPtr wszStoreName = IntPtr.Zero;

	            try
	            {
		            // allocate the store base name.
		            wszStoreName = DuplicateString(m_symbolicName);

                    hStore = NativeMethods.CertOpenStore(
                       new IntPtr(CERT_STORE_PROV_SYSTEM),
                       0, 
                       IntPtr.Zero,
                       GetFlags(m_storeType) | CERT_STORE_DELETE_FLAG, 
                       wszStoreName);

                    if (hStore == IntPtr.Zero)
                    {
                        int dwError = Marshal.GetLastWin32Error();

                        if (dwError != 0 && dwError != CRYPT_E_NOT_FOUND)
                        {
                            throw ServiceResultException.Create(
                                StatusCodes.BadUnexpectedError,
                                "Could not delete the certificate store.\r\nType={0}, Name={1}, Error={2:X8}", 
                                m_storeType,
                                m_symbolicName,
                                dwError);
                        }
                    }
	            }
	            finally
	            {
		            if (hStore != IntPtr.Zero)
                    {
                        int result = NativeMethods.CertCloseStore(hStore, CERT_CLOSE_STORE_CHECK_FLAG);

                        if (result == 0)
                        {
                            Utils.Trace("Could not close certificate store. Error={0:X8}", Marshal.GetLastWin32Error());
                        }
		            }

		            if (wszStoreName != IntPtr.Zero)
		            {
			            Marshal.FreeHGlobal(wszStoreName);
		            }
	            }
            }
        }