Permission Issue

MiPakTeh 1,476 Reputation points
2021-08-26T01:48:51.923+00:00

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));
            }));
        }

    }
}
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,101 questions
{count} votes

Accepted answer
  1. Jack J Jun 24,606 Reputation points Microsoft Vendor
    2021-08-27T01:47:59.673+00:00

    @MiPakTeh , 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.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.