如何解决我正在尝试检查服务器是否已启动或未使用网络连接,但似乎我的参数没有启动
我在这里做的是检查服务器网络是否已启动。对于这个例子,我正在检查我的本地主机,这就是为什么我使用 127.0.0.1 本地主机和我的计算机用户登录名和密码登录名的原因。但随后观察过程,它不接受凭据,即使我已经正确输入了凭据。请问我该怎么做?
我已经尝试将它们放在 '' 或 @'' 我也在双引号 "" 或 @"" 中这样做仍然不起作用所以我像这样添加一个 "\" 或 @"\" 双黑斜线仍然不起作用我用完了关于如何的选项,因为我用作代码参考的文档显示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,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
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。