smitha
smitha

Reputation: 59

Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)

code:

ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
ManagementObjectCollection queryCollection = searcher.Get();

Above code throws the following exception please Help..i read in some article that this error are thrown due to WMI corruption?Is this the case?do i have to rebuild WMI or is there any alternate?or a better solution

Error:

Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)

System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo) at System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(Int32 errorCode) at System.Management.ManagementScope.InitializeGuts(Object o) at System.Management.ManagementScope.Initialize() at System.Management.ManagementScope.Connect() at WebApplication3.lanusers.Button1_Click1(Object sender, EventArgs e) in C:\Users\user\documents\visual studio 2010\Projects\WebApplication3\WebApplication3\lanusers.aspx.cs:line 120 IpAddress192.168.1.55HostNamenkn-PC

Upvotes: 4

Views: 177455

Answers (6)

U can use this code. It works to me

 void Close(string remoteComputer, string appName)
 {
     ConnectionOptions connectionOptions = new ConnectionOptions();
     connectionOptions.Username = "USERNAME";
     connectionOptions.Password = "PASSWORD";
     connectionOptions.Authority = $"ntlmdomain:{Environment.MachineName}";

     ManagementScope scope = new ManagementScope($@"\\{remoteComputer}\root\cimv2", connectionOptions);

     ObjectQuery query = new ObjectQuery($"SELECT * FROM Win32_Process WHERE Name = '{appName}'");
     ManagementObjectSearcher search = new ManagementObjectSearcher(scope, query);
     ManagementObjectCollection processes = search.Get();

     foreach (ManagementObject process in processes)
     {
         try
         {
             process.InvokeMethod("Terminate", null);
         }
         catch (Exception ex)
         {
             //do what you want
         }
     }
 }

Upvotes: 0

Neelam Prajapati
Neelam Prajapati

Reputation: 3802

tick checkbox of capabilities in appxmanifest in case of UWP app.

Upvotes: 0

kenorb
kenorb

Reputation: 166409

The error 0x80070005 usually relates to permission denied to the cache files, so make sure they've the right permissions.

For example go to /Users/Public/Application Data/Package Cache folder (or other folder) and make sure that the folders and files there has the right permissions. Otherwise find out which temporary/cache files are causing the issue and remove them.

Upvotes: 0

Somnath
Somnath

Reputation: 1

Also Please allow the app "windows management instrumentation (wmi)" to communicate through windows firewall. Please refer this link for more info. https://social.msdn.microsoft.com/Forums/vstudio/en-US/6229334e-d5ef-4016-9e7e-1c8718be8d43/access-is-denied-exception-from-hresult-0x80070005-eaccessdenied-in-vbnet?forum=netfxbcl

Upvotes: 0

ali hesham
ali hesham

Reputation: 11

Did you try enabling Anonymous access for the site in IIS?

Edit:

The error message clearly says what you need to do.

Use a separate limited account for the site if you want or enable anonymous access for the site in IIS.

Upvotes: 0

RRUZ
RRUZ

Reputation: 136391

The Error code 0x80070005 is related to the WMI permissions.

0x8007xxx : Errors originating in the core operating system. WMI may return this type of error because of an external failure, for example, DCOM security failure.

In order to fix this error you must set the proper permissions in the client and server machines. Try these links

Upvotes: 6

Related Questions