Entwickler-Ecke

IO, XML und Registry - Prozess auslesen....


tomycat - So 26.04.20 11:08
Titel: Prozess auslesen....
hallo,
ich möchte mir gerne den notepad Prozess auslesen lassen ...


C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
using System;
using System.Diagnostics;
using System.ComponentModel;

namespace MyProcessSample
{
    class MyProcess
    {
        void BindToRunningProcesses()
        {
            // Get the current process.
            Process currentProcess = Process.GetCurrentProcess();

            // Get all processes running on the local computer.
            Process[] localAll = Process.GetProcesses();

            // Get all instances of Notepad running on the local computer.
            // This will return an empty array if notepad isn't running.
            Process[] localByName = Process.GetProcessesByName("notepad");

            // Get a process on the local computer, using the process id.
            // This will throw an exception if there is no such process.
            Process localById = Process.GetProcessById(1234);     ///////////////////// Fehler:{"Es wird kein Prozess mit der ID 1234 ausgeführt."}

            // Get processes running on a remote computer. Note that this
            // and all the following calls will timeout and throw an exception
            // if "myComputer" and 169.0.0.0 do not exist on your local network.

            // Get all processes on a remote computer.
            Process[] remoteAll = Process.GetProcesses("myComputer");

            // Get all instances of Notepad running on the specific computer, using machine name.
            Process[] remoteByName = Process.GetProcessesByName("notepad""myComputer");

            // Get all instances of Notepad running on the specific computer, using IP address.
            Process[] ipByName = Process.GetProcessesByName("notepad""169.0.0.0");

            // Get a process on a remote computer, using the process id and machine name.
            Process remoteById = Process.GetProcessById(2345"myComputer");
        }

        static void Main()
        {
            MyProcess myProcess = new MyProcess();
            myProcess.BindToRunningProcesses();
        }
    }
}

System.ArgumentException ----> Fehler:{"Es wird kein Prozess mit der ID 1234 ausgeführt."}

Idee ?

Moderiert von user profile iconTh69: <csharp>- durch C#-Tags ersetzt
Moderiert von user profile iconTh69: C#-Tags hinzugefügt


Ralf Jansen - So 26.04.20 11:27

Die Stelle die du als die Fehlerzeile kommentierst hast ist die Zeile in der drüber ein Kommentar steht der erklärt warum das passiert :?:


tomycat - So 26.04.20 16:10

thx,
nomalerweise vergibt doch Windows selbst eine Prozess ID?!
Ich nehme an, mit der Funktion kann man selbst eine Prozess ID hinzufügen. Warum geht 1234 nicht?
Oder ich mache einen Denkfehler?!


Th69 - So 26.04.20 16:19

Ja, du verstehst die Funktion falsch. Damit wird keine ID vergeben, sondern damit wird der Prozess gesucht, der die übergebene Id hat (und wenn dieser nicht existiert, eben eine Exception geworfen). Es macht also keinen Sinn, im Programmcode nach einer festen Id zu suchen (da diese von Windows bei jedem Programmstart selbständig vergeben wird - schau einfach im Task-Manager nach, evtl. mußt du "PID" noch als Spalte hinzufügen).

Was ist denn der Sinn bei deinem Code alle möglichen Process-Methoden auszuprobieren?


Ralf Jansen - So 26.04.20 22:44

Zitat:
Was ist denn der Sinn bei deinem Code alle möglichen Process-Methoden auszuprobieren?


Jemanden die ganzen möglichen zu zeigen die die Process Klasse so hergibt. Das ist das Standardbeispiel kopiert aus der Dokumentation.


Th69 - Mo 27.04.20 07:20

Ach, ich hatte nur das Beispiel auf der Hauptseite Process [https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.process] gesehen, nicht bei den einzelnen Methoden z.B. Process.GetProcessesByName [https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.process.getprocessesbyname].


tomycat - Mi 29.04.20 09:28

Das ist richtig, ich habe das Beispiel von der MS Doku rauskopiert.
sorry, ich habe falsch angesetzt.
Ich wollte sehen wann der Prozess notepad gestartet wird.


Th69 - Mi 29.04.20 11:15

Entweder dann mittels eines Timers immer wieder nach dem Notepad-Process suchen oder aber per WMI auf das passende EventArrived-Ereignis reagieren (Is there a System event when processes are created? [https://stackoverflow.com/questions/972039/is-there-a-system-event-when-processes-are-created] sowie C# - Gestartete und beendete Prozesse registrieren [https://dotnet-snippets.de/snippet/gestartete-und-beendete-prozesse-registrieren/3862] - erfordert aber wohl Adminrechte).