Just nu i M3-nätverket
Jump to content

Blockera program från att starta från en wpf-klient


m8Andersson

Recommended Posts

På jobbet håller jag på och utvecklar en wpf-klient i C#. Jag skall bland annat blockera program från att starta. Helst skall detta ske direkt när en användare försöker att starta programmet.

 

Vi har olika användare som skall kunna blockera olika program. Användarna loggar in i wpf-klienten. Vilka program som är blockerade finns i en databas som min wpf-klient kan läsa.

 

 

Tacksam för tips om hur jag skall lösa problemet

Link to comment
Share on other sites

I am trying this solution. In this test solution i kill all application that starts. If you see anything strange or something that is wrong. Let me know

 

// I call this method when i start my wpf-client

private bool Run()

{

bool bRun = false;

 

try

{

WqlEventQuery query = new WqlEventQuery("__InstanceCreationEvent", new TimeSpan(0, 0, 1), "TargetInstance isa \"Win32_Process\"");

 

m_Watcher = new ManagementEventWatcher(query);

m_Watcher.Options.Timeout = new TimeSpan(0, 0, 5);

m_Watcher.EventArrived += new EventArrivedEventHandler(HandleEvent);

m_Watcher.Start();

}

catch (Exception exc)

{}

return bRun;

}// End of Run

 

 

// Method that handle events from ManagementEventWatcher

 

private void HandleEvent(object sender,EventArrivedEventArgs e)

{

try

{

ManagementEventWatcher watch = sender as ManagementEventWatcher;

ManagementBaseObject manBasObj = null;

String strName = String.Empty;

String strPath = String.Empty;

String strPID = String.Empty;

 

if (watch != null)

{

Object obj;

manBasObj = e.NewEvent;

 

obj = ((ManagementBaseObject)manBasObj["TargetInstance"])["Name"];

if (obj is String)

strName = obj.ToString();

 

obj = ((ManagementBaseObject)manBasObj["TargetInstance"])["ExecutablePath"];

if (obj is String)

strPath = obj.ToString();

 

obj = ((ManagementBaseObject)manBasObj["TargetInstance"])["processId"];

if (obj != null)

{

try

{

strPID = obj.ToString();

}

catch (Exception exc){}

}

 

//str = String.Format("Process {0} has been created, path is: {1} , PID: {2}",strName,strPath,strPID);

 

KillProcess(strName, strPath, strPID);

}

 

}

catch (Exception exc)

{

}

}// End of HandleEvent

 

// Method that kill the process

 

private void KillProcess(String strName, String strPath, String strPid)

{

try

{

Process proc = null;

 

if (!String.IsNullOrEmpty(strPid))

{// We have PID

proc = Process.GetProcessById(int.Parse(strPid));

proc.Kill();

}

 

// If you want to kill process by name we can do this

/*Process[] pro;

if(!String.IsNullOrEmpty(strName))

{// We have name

pro = Process.GetProcessesByName(strName);

 

foreach (Process p in pro)

{

p.Kill();

}

 

}*/

}

catch (Exception exc)

{

}

}// End of KillProcess

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.



×
×
  • Create New...