Calculating server uptime gives “The network path was not found”

南笙酒味 提交于 2020-01-04 05:38:23

问题


For the following code, I am getting

System.ComponentModel.Win32Exception: The network path was not found

Can anyone help?

PerformanceCounter pc = new PerformanceCounter("System",
        "System Up Time");
                pc.MachineName = "1.2.3.4";

                //Normally starts with zero. do Next Value always.
                pc.NextValue();
                TimeSpan ts = TimeSpan.FromSeconds(pc.NextValue());

                Response.Write("This system 1.2.3.4 has been up for " + ts.Days + " days " + ts.Hours + " hours, " + ts.Minutes + " and " + ts.Seconds +" seconds.");

Edit: I tried with machine name, and I still get the same error! Note: 1.2.3.4 is a sample value.


回答1:


Uncomment pc.NextValue() and the code works. The problem is reproduceable by giving a bad machine name or IP address. So your IP is bad.

var machineNameOrIP = "10.16.7.1";
var pc = new PerformanceCounter("System", "System Up Time");
pc.MachineName = machineNameOrIP;
//Normally starts with zero. do Next Value always.
pc.NextValue();//uncomment this
var ts = TimeSpan.FromSeconds(pc.NextValue());
Response.Write("This system " + pc.MachineName + " has been up for " + ts.Days + " days " + ts.Hours + " hours, " + ts.Minutes + " and " + ts.Seconds +" seconds.");



回答2:


This error can be caused if the Remote Registry service is not started on the remote machine 1.




回答3:


Either the MachineName "1.2.3.4" does not exist, or it cannot be reached on the network.

I tested your code with a machine name that exists on my network, and it works fine.




回答4:


More than likely the MachineName value that you are using is not a machine that can be found on the network. i would start with that and validate that you are able to connect to it.



来源:https://stackoverflow.com/questions/4785717/calculating-server-uptime-gives-the-network-path-was-not-found

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!