标签: system.diagnostics

如何找到活动(正在使用)图形卡?C#

我使用此代码查找显卡:

        ManagementScope scope = new ManagementScope("\\\\.\\ROOT\\cimv2");
        ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_VideoController");
        ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
        ManagementObjectCollection queryCollection = searcher.Get();
        string graphicsCard = "";
        foreach (ManagementObject mo in queryCollection)
        {
            foreach (PropertyData property in mo.Properties)
            {
                if (property.Name == "Description")
                {
                    graphicsCard += property.Value.ToString() + " ";
                }
            }
        }
Run Code Online (Sandbox Code Playgroud)

我有两张显卡:

我的显卡

上面的代码返回所有图形卡。

如何找到windows选择的活动显卡?

c# wmi system.diagnostics nvidia

4
推荐指数
1
解决办法
1316
查看次数

WorkingDirectory的System.Diagnostics.Process问题

我正在使用第三方软件工具(命令行工具)将PDF文件合并在一起.使用C#我试图用来System.Diagnostics.Process运行可执行文件,但我想出了一些错误,具体取决于参数设置.

  • 如果UseShellExecute = trueRedirectStandardOutput = true我得到:
    • Process对象必须将UseShellExecute属性设置为false以重定向IO流.
  • 如果UseShellExecute = trueRedirectStandardOutput = false我得到:
    • 该系统找不到指定的文件
  • 如果useShellExecute = falseRedirectStandardOutput = true我得到:
    • 该系统找不到指定的文件
  • 如果UseShellExecute = falseRedirectStandardOutput = false我得到:
    • 该系统找不到指定的文件

正在运行的代码如下:

Process p = new Process();

p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = false;
p.StartInfo.WorkingDirectory = "C:\\Program Files (x86)\\VeryPDF PDF Split-Merge v3.0";
p.StartInfo.FileName = "pdfpg.exe " + strFileNames.Trim() + " " 
                       + D2P_Folder_Converted + "\\" …
Run Code Online (Sandbox Code Playgroud)

system.diagnostics process processstartinfo

3
推荐指数
1
解决办法
8537
查看次数

将Trace方法添加到System.Diagnostics.TraceListener

我写了一个从System.Diagnostics.TraceListener派生的Log类,就像这样

public class Log : TraceListener
Run Code Online (Sandbox Code Playgroud)

这充当了Log4Net的包装器,允许人们像这样使用System.Diagnostics Tracing

Trace.Listeners.Clear();
Trace.Listeners.Add(new Log("MyProgram"));
Trace.TraceInformation("Program Starting");
Run Code Online (Sandbox Code Playgroud)

有一个请求添加其他跟踪级别,然后是默认跟踪级别(错误,警告,信息)

我希望将此添加到System.Diagnostics.Trace中,以便可以像使用它一样使用

Trace.TraceVerbose("blah blah");
Trace.TraceAlert("Alert!");
Run Code Online (Sandbox Code Playgroud)

有什么方法可以用扩展类做到这一点吗?我试过了

public static class TraceListenerExtensions
{
     public static void TraceVerbose(this Trace trace) {}
}
Run Code Online (Sandbox Code Playgroud)

但没有任何东西被传递到传入的跟踪实例:(

.net c# system.diagnostics

3
推荐指数
1
解决办法
6732
查看次数

如果类别不存在,PerformanceCounterCategory.Exists非常慢

我有一种库,它使用一堆自己的perf计数器.但我希望我的库工作正常,即使没有安装perf计数器.

所以我已经在PerformanceCounter上创建了包装器,并在第一次使用时检查PerfCounter是否存在.如果它们存在,那么我使用本机PerformanceCounter而不是我使用的包装器什么都不做.

因此,为了检查perf计数器的存在,我使用了PerformanceCounterCategory.Exists

问题是,如果没有这样的类别,那么PerformanceCounterCategory.Exists调用(在我的机器上)大约需要10秒!不用说它太慢了.

我能做什么?

代码自己尝试:使用System; 使用System.Diagnostics;

class Program
{
    static void Main(string[] args)
    {
        var ts = Stopwatch.StartNew();
        var res = PerformanceCounterCategory.Exists("XYZ");
        Console.WriteLine(ts.ElapsedMilliseconds);
        Console.WriteLine("result:" + res);
}
}
Run Code Online (Sandbox Code Playgroud)

.net system.diagnostics

3
推荐指数
1
解决办法
1689
查看次数

使用System.Diagnostics.Process.Start运行程序会导致应用程序错误

在我的电脑上打开DWG文件:

"C:\Program Files\AutoCAD LT 2007\acadlt.exe" "%1"
Run Code Online (Sandbox Code Playgroud)

如果我从命令行运行它:

"C:\Program Files\AutoCAD LT 2007\acadlt.exe" "C:\Some Path\Test.dwg"
Run Code Online (Sandbox Code Playgroud)

AutoCAD Lite打开DWG文件.

类似地,如果我打开命令提示符并使用参数运行相同的exe,它可以正常工作.

但是,如果我使用

var proc = new System.Diagnostics.Process();
var info = new System.Diagnostics.ProcessStartInfo();
Run Code Online (Sandbox Code Playgroud)

然后

info.FileName = "C:\Some Path\Test.dwg";
proc.StartInfo = info;
proc.Start();
Run Code Online (Sandbox Code Playgroud)

要么

info.FileName = "C:\Program Files\AutoCAD LT 2007\acadlt.exe";
info.Arguments= "C:\Some Path\Test.dwg"
proc.StartInfo = info;
proc.Start();
Run Code Online (Sandbox Code Playgroud)

要么

info.FileName = "cmd.exe";
info.Arguments= "C:\Program Files\AutoCAD LT 2007\acadlt.exe" "C:\Some Path\Test.dwg"
proc.StartInfo = info;
proc.Start();
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:


acadlt.exe - 应用程序错误

"0x01317c8c"处的指令引用"0x01317c8c"处的存储器.内存无法"读取".

单击OK以终止程序单击CANCEL以调试程序

确定取消


顺便提一下,如果我使用调试器逐步执行代码,代码就可以了.

有谁知道如何使用Process.Start打开这个DWG?

c# system.diagnostics

3
推荐指数
1
解决办法
5643
查看次数

从Windows 7通过Process.GetProcesses(字符串)读取进程信息的异常

我在使用已建立的生产.NET2服务时遇到了麻烦.该服务从各种目标计算机收集进程信息和日志文件.它运行在80个站点没有问题; 主要在Windows 2000,Windows XP和Windows 2003上.

现在,针对Windows 7目标运行,只要服务尝试读取进程信息,就会发生异常.

代码如下:

 Process[] procs = System.Diagnostics.Process.GetProcesses("10.11.12.13");
Run Code Online (Sandbox Code Playgroud)

信息:目标计算机响应ping并且目标计算机和服务计算机上都存在"adminUser"凭据.

例外信息:

ex {"无法连接到远程计算机."} System.Exception {System.InvalidOperationException} [System.InvalidOperationException] {"无法连接到远程计算机."} System.InvalidOperationException

Data {System.Collections.ListDictionaryInternal} System.Collections.IDictionary {System.Collections.ListDictionaryInternal}

InnerException {"无法从性能计数器获取进程信息."} System.Exception {System.InvalidOperationException} [System.InvalidOperationException] {"无法从性能计数器获取进程信息."} System.InvalidOperationException

Data {System.Collections.ListDictionaryInternal} System.Collections.IDictionary {System.Collections.ListDictionaryInternal}

InnerException {"找不到网络路径"} System.Exception {System.ComponentModel.Win32Exception}

消息"无法从性能计数器获取进程信息." 串

源"系统"字符串

StackTrace"在System.Diagnostics.NtProcessManager.GetProcessInfos(PerformanceCounterLiblibrary)\ r \n在System.Diagnostics.NtProcessManager.GetProcessInfos(String machineName,Boolean isRemoteMachine)"string

TargetSite {System.Diagnostics.ProcessInfo [] GetProcessInfos(System.Diagnostics.PerformanceCounterLib)} System.Reflection.MethodBase {System.Reflection.RuntimeMethodInfo}

消息"无法连接到远程计算机".串

要解决此问题,我在目标计算机上禁用了Windows防火墙,但无济于事.有人有主意吗?


如果有人对我应该尝试的步骤和顺序有任何建议,我非常感谢你的帮助.


更新:我从监控计算机执行了"tasklist"命令,传递参数以查询远程(目标)计算机,我能够看到相同类型的过程信息,我无法以编程方式获取...

该命令看起来像:

tasklist /s 10.11.12.13
Run Code Online (Sandbox Code Playgroud)

返回的信息如下:

...  
notepad.exe                    672                            1      4,916 K
...  
Run Code Online (Sandbox Code Playgroud)

那么,为什么.NET不能看到进程信息???

.net c# system.diagnostics windows-7

3
推荐指数
1
解决办法
4987
查看次数

使用System.Diagnostics.Process启动Jar文件

我有一个jar文件,我想在C#中运行.

这是我到目前为止所拥有的:

clientProcess.StartInfo.FileName = @"java -jar C:\Users\Owner\Desktop\myJarFile.jar";
            clientProcess.StartInfo.Arguments = "[Something]";

            clientProcess.Start();
            clientProcess.WaitForExit();

            int exitCode = clientProcess.ExitCode;
Run Code Online (Sandbox Code Playgroud)

不幸的是,我得到"系统找不到指定的文件",这是有道理的,因为它不是一个文件的命令.

我见过在线代码告诉你使用:

System.Diagnostics.Process.Start("java -jar myprog.jar");
Run Code Online (Sandbox Code Playgroud)

但是我需要返回代码,我需要等待它退出.

谢谢.

c# java system.diagnostics

3
推荐指数
1
解决办法
1万
查看次数

EventSource .net 4.0 GenerateManifest

我一直在尝试在.net 4.0中使用ETW.

我已经开始使用Microsoft EventSource Library 1.0.4-beta(https://www.nuget.org/packages/Microsoft.Diagnostics.Tracing.EventSource)

这是我为我的应用程序生成事件而编写的代码.

[EventSource(Name = "Samples-EventSourceDemos-EventSourceLogger")]
public sealed class EventSourceLogger : EventSource
{
    public static EventSourceLogger Log = new EventSourceLogger();

    public static string GetManifest()
    {
        return GenerateManifest(typeof(EventSourceLogger), null);
    }

    [Event(200, Level = Microsoft.Diagnostics.Tracing.EventLevel.Informational, Task = EventTask.None, Version = 1,
        Opcode = EventOpcode.Info, Keywords = EventKeywords.None, Channel = EventChannel.Admin,
        Message = "Test Message")]
    public void LogEtwInfoEventMessage(string jsonArgs)
    {
        if (!this.IsEnabled()) return;

        this.WriteEvent(200, jsonArgs);
    }

    [Event(400, Level = Microsoft.Diagnostics.Tracing.EventLevel.Error, Task = EventTask.None, Version = 1,
        Opcode …
Run Code Online (Sandbox Code Playgroud)

system.diagnostics etw .net-4.0 event-log etw-eventsource

3
推荐指数
1
解决办法
3142
查看次数

在接收到新行之前读取进程 StandardOutput

我正在尝试做一些似乎超出 System.Diagnostics.Process 对象范围的事情。可接受的答案可以提出不同的方法,只要它使用 .net 4.5/c#5。

我的程序正在调用 gdalwarp.exe 对大型 tiff 文件执行长时间运行的进程。Galwarp.exe 以这种格式输出。

Creating output file that is 6014P x 4988L.  
Processing input file [FileName].tiff. 
Using band 4 of source image as alpha. 
Using band 4 of destination image as alpha.
0...10...20...30...40...50...60...70...80...90...100 - done.
Run Code Online (Sandbox Code Playgroud)

最后一行缓慢流入以指示进展。我想在它发生变化时阅读该行,以便我可以移动进度条以通知用户。

首先,我尝试读取,Process.StandardOutput但在整个过程完成之前它不会提供任何数据。其次,我尝试调用Process.BeginOutputReadLine()并连接该事件,Process.OutputDataReceived但它仅在线路完成时触发。

这是对 Execute GDalWarp.exe 的调用。

    public static void ResizeTiff(string SourceFile, string DestinationFile, float ResolutionWidth, float ResolutionHeight, Guid ProcessId)
    {
        var directory = GDalBin;
        var exe = Path.Combine(directory, "gdalwarp.exe");
        var args = …
Run Code Online (Sandbox Code Playgroud)

c# system.diagnostics

3
推荐指数
1
解决办法
2542
查看次数

启动.exe文件而不用路径c#

我如何使用c#代码启动exe文件?所以我有这个:

Process.Start( @"C:\Program Files (x86)\Photoshop\Photoshop.exe");
Run Code Online (Sandbox Code Playgroud)

但是其他机器的路径可能不同.那么有什么想法以不同的方式运行.exe吗?

谢谢!

c# system.diagnostics process c#-6.0

3
推荐指数
1
解决办法
493
查看次数