Autor Beitrag
tomycat
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 265
Erhaltene Danke: 1



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

ausblenden volle Höhe 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
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 4700
Erhaltene Danke: 991


VS2010 Pro, VS2012 Pro, VS2013 Pro, VS2015 Pro, Delphi 7 Pro
BeitragVerfasst: 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 :?:

Für diesen Beitrag haben gedankt: tomycat
tomycat Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 265
Erhaltene Danke: 1



BeitragVerfasst: 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
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4764
Erhaltene Danke: 1052

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: 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?

Für diesen Beitrag haben gedankt: tomycat
Ralf Jansen
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 4700
Erhaltene Danke: 991


VS2010 Pro, VS2012 Pro, VS2013 Pro, VS2015 Pro, Delphi 7 Pro
BeitragVerfasst: 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.

Für diesen Beitrag haben gedankt: tomycat
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4764
Erhaltene Danke: 1052

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Mo 27.04.20 07:20 
Ach, ich hatte nur das Beispiel auf der Hauptseite Process gesehen, nicht bei den einzelnen Methoden z.B. Process.GetProcessesByName.

Für diesen Beitrag haben gedankt: tomycat
tomycat Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 265
Erhaltene Danke: 1



BeitragVerfasst: 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
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4764
Erhaltene Danke: 1052

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: 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? sowie C# - Gestartete und beendete Prozesse registrieren - erfordert aber wohl Adminrechte).

Für diesen Beitrag haben gedankt: tomycat