提问人:Mik AA 提问时间:6/28/2021 最后编辑:Mik AA 更新时间:6/28/2021 访问量:285
我正在尝试检查服务器是否已启动或未使用网络连接,但似乎我的参数没有启动
I'm trying to check if a server is up or not using Network Connection but seems like my parameters are not kicking in
问:
我在这里做的是检查服务器网络是否正常。对于这个例子,我正在检查我的localhost,这是我使用127.0.0.1 localhost和我的计算机用户登录名和密码登录的原因。但是,然后观察该过程,即使我已经正确输入了凭据,它也不接受凭据。请问我该怎么做呢?
我已经尝试将它们放在“”或@''上,我也用双引号“”或@“”仍然不起作用,所以我像这样添加“\”或@“\”双黑斜杠仍然不起作用,我用完了关于如何的选项,因为我用作代码参考的文档显示IP地址应该用于服务器的名称,只是添加一个引号和@和\,它不会为我工作。
SIPrintServiceSettings _SIpss = new SIPrintServiceSettings();
appConfig.GetSection("AppSettings").GetSection("PrintServeSettings").Bind(_SIpss);
//AppSettings is a JSON File
//Set Configuration
remoteMachine = "127.0.0.1";
userName = "Administrator";
password = "12345";
using (new NetworkConnection(@remoteMachine, new NetworkCredential(@userName, @password)))
{
ServiceController sc = new ServiceController(SysAppName, remoteMachine); if ((sc.Status.Equals(ServiceControllerStatus.Stopped)) || (sc.Status.Equals(ServiceControllerStatus.StopPending)))
{
//invoke slave here
executeProcessInformation();
}
}
我在NetworkConnection.cs上有这个代码
using System;
using System.ComponentModel;
using System.Net;
using System.Runtime.InteropServices;
/// <summary>
/// Represents a network connection along with authentication to a network share.
/// </summary>
public class NetworkConnection : IDisposable
{
string _networkName;
public NetworkConnection(string networkName,
NetworkCredential credentials)
{
_networkName = networkName;
var netResource = new NetResource()
{
Scope = ResourceScope.GlobalNetwork,
ResourceType = ResourceType.Disk,
DisplayType = ResourceDisplaytype.Share,
RemoteName = networkName
};
var userName = string.IsNullOrEmpty(credentials.Domain)
? credentials.UserName
: string.Format(@"{0}\{1}", credentials.Domain, credentials.UserName);
var result = WNetAddConnection2(
netResource,
credentials.Password,
userName,
0);
if (result != 0)
{
throw new Win32Exception(result);
}
}
~NetworkConnection()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
WNetCancelConnection2(_networkName, 0, true);
}
[DllImport("mpr.dll")]
private static extern int WNetAddConnection2(NetResource netResource,
string password, string username, int flags);
[DllImport("mpr.dll")]
private static extern int WNetCancelConnection2(string name, int flags,
bool force);
}
[StructLayout(LayoutKind.Sequential)]
public class NetResource
{
public ResourceScope Scope;
public ResourceType ResourceType;
public ResourceDisplaytype DisplayType;
public int Usage;
public string LocalName;
public string RemoteName;
public string Comment;
public string Provider;
}
public enum ResourceScope : int
{
Connected = 1,
GlobalNetwork,
Remembered,
Recent,
Context
};
public enum ResourceType : int
{
Any = 0,
Disk = 1,
Print = 2,
Reserved = 8,
}
public enum ResourceDisplaytype : int
{
Generic = 0x0,
Domain = 0x01,
Server = 0x02,
Share = 0x03,
File = 0x04,
Group = 0x05,
Network = 0x06,
Root = 0x07,
Shareadmin = 0x08,
Directory = 0x09,
Tree = 0x0a,
Ndscontainer = 0x0b
}
答: 暂无答案
评论