Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 捕获在运行时启动的进程的GUID_C#_Guid - Fatal编程技术网

C# 捕获在运行时启动的进程的GUID

C# 捕获在运行时启动的进程的GUID,c#,guid,C#,Guid,我不确定如何获取在运行时启动的进程的GUID。例如: System.Diagnostics.Process proc = new System.Diagnostics.Process(); proc.StartInfo.WorkingDirectory = @"C:\MyDir"; proc.StartInfo.FileName = "MyApp.exe"; proc.StartInfo.Arguments = "E F G H"; proc.Start(); 我在另一个公开当前应用程序GUI

我不确定如何获取在运行时启动的进程的GUID。例如:

System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.WorkingDirectory = @"C:\MyDir";
proc.StartInfo.FileName = "MyApp.exe";
proc.StartInfo.Arguments = "E F G H";
proc.Start();
我在另一个公开当前应用程序GUID的线程中发现了以下代码:

var assembly = typeof(Program).Assembly;
var attribute = (GuidAttribute)assembly.GetCustomAttributes(typeof(GuidAttribute), true)[0];
var id = attribute.Value;

但我不确定如何将此应用于
proc
,或者proc中是否有显示其GUID的属性。如何获取
proc
的GUID?

进程没有GUID

可以使用反射从托管程序集中的已知类检索属性:

Assembly.Load(path).GetType("Namespace.Type")
                   .GetCustomAttributes(typeof(GuidAttribute), true)[0]

如果要查找进程的ID,可以使用
proc.ID
获取属性


请参见

您提供的代码不是OP想要的,它用于获取程序集中某种类型的属性。@KingKing您能澄清一下吗?这似乎回答了我的问题。它还解释了进程没有分配GUID。@SimonWhitehead第一个可能看起来不错,但代码显然用于获取某种类型的GUID属性,我认为OP可能想表示他想要获取exe文件的GUID,而exe文件也是一个程序集,所以为什么它没有GUID?当然,他指的不是进程,他可能指的是exe文件是的,它是一个exe文件。在我的例子中,MyApp.exe正在调用自己。应用程序正在使用企业库进行日志记录,递归执行中生成的日志无法记录到同一个日志文件,因此它创建了一个新的日志文件,文件名中包含创建应用程序的GUID。与其使用不同的日志系统,我只想记录作为引用启动的exe的GUID。@看起来您可以使用
Assembly.Load
Assembly.LoadFile
获取物理exe文件的相应程序集的实例,并在代码中使用该实例(替换
typeof(Program.Assembly
)这是我开始的地方,但是proc.id返回的整数不是我需要的。