question

MiPakTeh-6272 avatar image
0 Votes"
MiPakTeh-6272 asked MiPakTeh-6272 commented

Permission Issue

Hi All,

Just to look at permission .
I ran the program as Administrator and the issue was gone. If I want it can be run in other user , it possible use a
UnauthorizedAccessException ?.

Let said it not secure in LocalSystem.

 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using System.Windows.Forms;
 using System.Management;
 using System.Diagnostics;
 using System.Threading;
 using System.IO;
    
 namespace mag_3
 {
     public partial class Form1 : Form
     {
         public Form1()
         {
             InitializeComponent();
             ManagementEventWatcher stopWatch = new ManagementEventWatcher(new WqlEventQuery("SELECT * FROM Win32_ProcessStopTrace"));
    
             try
             {
                 stopWatch.EventArrived += new EventArrivedEventHandler(this.Bulk_);
                 stopWatch.Start();
             }
             catch (Exception)
             {
                 MessageBox.Show("Can't start the Process-Monitor! Are you running as admin?");
                 return;
             }
         }
    
         private void Bulk_(object sender, EventArrivedEventArgs e)
         {
             Invoke(new MethodInvoker(() =>
             {
                 listBox1.Items.Add(string.Format("Process stop: {0}", e.NewEvent.Properties["Second.exe"].Value));
             }));
         }
    
     }
 }



dotnet-csharp
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@MiPakTeh-6272, I am not full understand your problem. Do you want to use the UnauthorizedAccessException for the access denied error or just want to solve the issue when the app iruns in other user)? Could you describe it in more detail?

0 Votes 0 ·

Hi JackJJun;
Thank for your reply.
just want to solve the issue when the app runs in other user.It is correct or I don't want this program are secure and everybody can use
it.

0 Votes 0 ·

1 Answer

JackJJun-MSFT avatar image
0 Votes"
JackJJun-MSFT answered MiPakTeh-6272 commented

@MiPakTeh-6272 , It is necessary for the app to have the Admin Power if we want to use WMI to monitor if the process is stopped.

Therefore, we have to force the app to run as Administrator all the time.

Please refer to the following steps to do it.

First, Please add a Application Manifest File in your app.

Second, Please modify the code in the manifest file like the following:

 <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

Third, Please restart your vs 2019.

Finally, the app can be used it in everybody with the Administrator Power.


If the response is helpful, please click "Accept Answer" and upvote it.


Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.






· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

using method manifest file succeed and Excelence.


But one more thing, If in stopWatch.EventArrived we want only specified file to show in ListBox ,How to do'

private readonly string exeName = @"C:\Users\family\Desktop\Second.exe";

         private void Bulk_(object sender, EventArrivedEventArgs e)
         {
             //Console.WriteLine("Process stopped: {0}", e.NewEvent.Properties["ProcessName"].Value);
    
             string processname = (string)e.NewEvent.Properties["ProcessName"].Value;
             if (processname.ToLower().Contains(exeName))
             {
                 Invoke(new MethodInvoker(() =>
                 {
                     listBox1.Items.Add(string.Format("Process stop: {0}", processname));
                 }));
             }
    
         }
0 Votes 0 ·

Change the query to specify the executable of interest. For example,

 ManagementEventWatcher stopWatch = new ManagementEventWatcher(new WqlEventQuery("SELECT * FROM Win32_ProcessStopTrace WHERE ProcessName='Notepad.exe'"));


0 Votes 0 ·

Thank a lot.

0 Votes 0 ·