我正在尝试检查服务器是否已启动或未使用网络连接,但似乎我的参数没有启动

I'm trying to check if a server is up or not using Network Connection but seems like my parameters are not kicking in

提问人:Mik AA 提问时间:6/28/2021 最后编辑:Mik AA 更新时间:6/28/2021 访问量:285

问:

我在这里做的是检查服务器网络是否正常。对于这个例子,我正在检查我的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
}

C# .NET .net-core 服务器端

评论

0赞 Jon Skeet 6/28/2021
没有迹象表明你为什么需要在这里逃跑。请花一些时间编辑您的问题,告诉我们您正在观察什么 - 很难理解您正在尝试做什么以及目前正在发生的事情。
0赞 Mik AA 6/28/2021
完成,先生,对不起,我不知道该问什么,但我真的需要这方面的帮助,这就是为什么如果我缺少有关该问题的内容,请告诉我,以便我可以清楚地说明。
0赞 jdweng 6/28/2021
使用 SQL Server Management Studio 连接到数据库以查看是否正常工作。在尝试使 c# 正常工作之前,首先需要使 SSMS 正常工作。SSMS 中的错误消息比 c# 更好,并且错误将解释问题。
0赞 Jon Skeet 6/28/2021
“但后来观察这个过程,它不接受证书”——你到底观察到了什么?发生了什么事?请阅读 codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question,了解如何写一个好问题的建议。
0赞 MD. RAKIB HASAN 6/28/2021
这可能会对您有所帮助。learn.microsoft.com/en-us/dotnet/api/......

答: 暂无答案