C# 使用C脚本和NAnt将文件复制到远程计算机(指定的登录会话不存在)

C# 使用C脚本和NAnt将文件复制到远程计算机(指定的登录会话不存在),c#,file,copy,nant,teamcity,C#,File,Copy,Nant,Teamcity,我目前正在使用TeamCity服务器上运行的NAnt和C编写一个脚本,用于在远程计算机上创建和部署MSI文件。远程计算机设置了一个共享文件夹,TeamCity服务器将通过脚本连接到该文件夹并将MSI文件复制到其中。我第一次运行它时它就可以正常工作了 但是当我第二次运行脚本时,当它试图连接到远程计算机上的共享时,它抛出一个异常,并显示以下消息: "A specified logon session does not exist. It may already have been terminate

我目前正在使用TeamCity服务器上运行的NAnt和C编写一个脚本,用于在远程计算机上创建和部署MSI文件。远程计算机设置了一个共享文件夹,TeamCity服务器将通过脚本连接到该文件夹并将MSI文件复制到其中。我第一次运行它时它就可以正常工作了

但是当我第二次运行脚本时,当它试图连接到远程计算机上的共享时,它抛出一个异常,并显示以下消息:

"A specified logon session does not exist. It may already have been terminated."
我使用来自线程的解决方案,使用登录证书将MSI文件从TeamCity服务器复制到远程计算机

以下是我的NAnt文件中的C脚本,它进行文件复制:

//Script main entry point
public static void ScriptMain(Project project)
{
    NetworkCredential deployTargetCridentials = new NetworkCredential(project.Properties["deploy.remotesvr.username"],
                                                                        project.Properties["deploy.remotesvr.password"]);

    string connection = @"\\" + project.Properties["deploy.remotesvr"] + project.Properties["deploy.remotesvr.sharepath"];

    string sourceFile = project.Properties["wix.output.dir"] + @"\" + project.Properties["wix.output.file"] + ".msi";
    string destinationFile = @"\\" + project.Properties["deploy.remotesvr"] +
                                        project.Properties["deploy.remotesvr.sharepath"] + @"\" +
                                        project.Properties["deploy.remotesvr.deployfile"];

    //Copy installation file to deploy share
    Copy(project
            , project.Properties["wix.output.dir"]
            , project.Properties["wix.output.file"] + ".msi"
            , @"\\" + project.Properties["deploy.remotesvr"] + project.Properties["deploy.remotesvr.sharepath"]
            , project.Properties["deploy.remotesvr.deployfile"]
            , deployTargetCridentials);

    ////
}


//Copy MSI
public static void Copy(Project project, string sourcePath, string sourceFile, string destinationPath, string destinationFile, NetworkCredential cridentials)
{
    string source = sourcePath + @"\" + sourceFile;
    string destination = destinationPath + @"\" + destinationFile;

    try
    {
        project.Log(Level.Info, " ");
        project.Log(Level.Info, "Copying " + source + " to " + destination);

        project.Log(Level.Info, "  Connecting to copy share: " + destinationPath);
        using (new NetworkConnection(destinationPath, cridentials))
        {
            project.Log(Level.Info, "  Copying file");
            File.Copy(source, destination, true);
        }
        project.Log(Level.Info, "Copy successfull!");
    }
    catch (Exception ex)
    {
        project.Log(Level.Warning, "WARNING: Could not copy file: " + ex.Message.ToString().Trim().Replace("\r\n", ""));
    }
}


////


//NetworkConnection class
public class NetworkConnection : IDisposable
{
    string _networkName;

    public NetworkConnection(string networkName, NetworkCredential credentials)
    {
        _networkName = networkName;

        NetResource netResource = new NetResource();
        netResource.Scope = ResourceScope.GlobalNetwork;
        netResource.ResourceType = ResourceType.Disk;
        netResource.DisplayType = ResourceDisplaytype.Share;
        netResource.RemoteName = networkName;

        int result = WNetAddConnection2(netResource, credentials.Password, credentials.UserName, 0);

        if (result != 0 && result != 1219)
        {
            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
}
副本中的下一行出现异常

using (new NetworkConnection(destinationPath, cridentials))
当我试图在测试应用程序中运行相同的复制代码时,它每次都复制文件。只有当我在脚本中运行它时,才会发生异常


有人知道这是为什么吗?

在打开新连接之前,您是否尝试过测试/关闭任何现有连接?

我尝试过在NetworkConnection的Dispose中删除对OwntcancelConnection2的调用,这导致它可以正常工作。因此,如果我取消连接,它会给我一个指定的登录会话不存在。它可能已经被终止了。第二次尝试打开与同一台计算机的连接时出错。我现在不太明白为什么..您是否也尝试连接\u更新\u配置文件而不是0?