Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#winforms程序:需要将文件复制到另一台windows PC上_C#_.net_Windows_File Transfer - Fatal编程技术网

C#winforms程序:需要将文件复制到另一台windows PC上

C#winforms程序:需要将文件复制到另一台windows PC上,c#,.net,windows,file-transfer,C#,.net,Windows,File Transfer,我正在尝试编写一个c#winform程序,可以将一些文件复制到另一台windows PC上。这两台PC都运行windows 7,但目标没有共享文件夹。 运行scp将文件复制到Linux设备上没有问题,但在Windows中没有SSH服务器在等待我:-( 我将有远程PC的登录详细信息(IP地址、用户名和密码),但我不知道在Windows环境中通常如何操作。 此程序的目的是自动将某些更新文件部署到多台计算机。我们将无法使用windows共享来传输这些文件,因为这并不比通过vnc手动登录和复制文件快。在

我正在尝试编写一个c#winform程序,可以将一些文件复制到另一台windows PC上。这两台PC都运行windows 7,但目标没有共享文件夹。 运行scp将文件复制到Linux设备上没有问题,但在Windows中没有SSH服务器在等待我:-( 我将有远程PC的登录详细信息(IP地址、用户名和密码),但我不知道在Windows环境中通常如何操作。
此程序的目的是自动将某些更新文件部署到多台计算机。我们将无法使用windows共享来传输这些文件,因为这并不比通过vnc手动登录和复制文件快。

在Internet上进行了一些研究,并在windows XP计算机上进行了实验(这是我碰巧拥有的唯一一台带有本地管理员帐户的机器——希望它能在Windows 7机器上正常工作)

最终,我意识到远程共享是正确的前进方向——感谢那些发表评论的人:——)

请注意,在示例代码中,我使用了一个名为mDisplay的类实例-它用于为我处理显示内容,例如显示错误消息或结果,而不是当前问题的核心

第一个问题-如何在没有手动干预的情况下在远程计算机上创建共享

public bool CreateShare(string ipAddress, string username, string password, string remoteFolder, string sharename)
{
    bool result = false;

    try
    {
        // Create management scope and connect to the remote machine
        ConnectionOptions options = new ConnectionOptions();
        options.Username = username;
        options.Password = password;
        string path = string.Format(@"\\{0}\root\cimv2", ipAddress);
        ManagementScope scope = new ManagementScope(path, options);
        scope.Connect();

        // http://www.c-sharpcorner.com/forums/creating-a-remote-share-using-wmi-in-c-sharp
        ManagementPath winSharePath = new ManagementPath("Win32_Share");
        ManagementClass winShareClass = new ManagementClass(scope, winSharePath, null);
        ManagementBaseObject shareParams = winShareClass.GetMethodParameters("Create");
        shareParams["Path"] = remoteFolder;
        shareParams["Name"] = sharename;
        shareParams["Type"] = 0;
        shareParams["Description"] = "Temporary folder share";
        ManagementBaseObject winShareResult = winShareClass.InvokeMethod("Create", shareParams, null);

        result = true;
    }
    catch (Exception ex)
    {
        mDisplay.showError("FAILED to create share: " + ex.Message.ToString());
    }

    return result;
}
第二个问题-将文件从本地计算机复制到远程计算机

感谢您提供此代码的基础。 我的版本不一定是做这项工作的最有效或最紧凑的方式,但它对我很有用

public bool CopyFiles(bool overwrite, string ipAddress, string sharename, string copySource)
{
    bool result = false;

    string path = string.Format(@"\\{0}\{1}", ipAddress, sharename);

    if (!Directory.Exists(path))
    {
        mDisplay.appendToResults("Share doesn't exist \"" + path + "\" - are you sure it mapped ok?");
        return result;
    }

    if (!Directory.Exists(copySource))
    {
        mDisplay.appendToResults("Source folder \"" + copySource + "\" doesn't exist, need to specify where to copy file(s) from");
        return result;
    }

    recursivelyCopyContents(copySource, overwrite, path);
    result = true;

    return result;
}
private void recursivelyCopyContents(string sourceDirName, bool overwrite, string destDirName)
{
    try
    {
        // Get the subdirectories for the specified directory.
        DirectoryInfo dir = new DirectoryInfo(sourceDirName);

        if (!dir.Exists)
        {
            throw new DirectoryNotFoundException(
                "Source directory does not exist or could not be found: "
                + sourceDirName);
        }

        DirectoryInfo[] dirs = dir.GetDirectories();
        // If the destination directory doesn't exist, create it.
        if (!Directory.Exists(destDirName))
        {
            mDisplay.appendToResults("Creating folder " + destDirName);
            Directory.CreateDirectory(destDirName);
        }

        // Get the files in the directory and copy them to the new location.
        FileInfo[] files = dir.GetFiles();
        foreach (FileInfo file in files)
        {
            string temppath = Path.Combine(destDirName, file.Name);

            string info = "Copying " + file.Name + " -> " + temppath;


            if (!overwrite && File.Exists(temppath))
            {
                // Not overwriting - so show it already exists
                mDisplay.appendToResults(info + " (already exists)");
            }
            else
            {
                file.CopyTo(temppath, overwrite);
                mDisplay.appendToResults(info + " OK");
            }
        }

        foreach (DirectoryInfo subdir in dirs)
        {
            string temppath = Path.Combine(destDirName, subdir.Name);
            recursivelyCopyContents(subdir.FullName, overwrite, temppath);
        }
    }
    catch (Exception ex)
    {
        mDisplay.showError("Problem encountered during copy : " + ex.Message);
    }
}
最后-删除我之前创建的远程共享,不希望它留在远程计算机上

public bool RemoveShare(string username, string password, string ipAddress, string sharename)
{
    bool result = false;

    try
    {
        // Create management scope and connect to the remote machine
        ConnectionOptions options = new ConnectionOptions();
        options.Username = username;
        options.Password = password;
        string path = string.Format(@"\\{0}\root\cimv2", ipAddress);
        ManagementScope scope = new ManagementScope(path, options);
        scope.Connect();

        // Inspiration from here: https://danv74.wordpress.com/2011/01/11/list-network-shares-in-c-using-wmi/
        var query = new ObjectQuery(string.Format("select * from win32_share where Name=\"{0}\"", sharename));
        var finder = new ManagementObjectSearcher(scope, query);
        var shares = finder.Get();

        foreach (ManagementObject share in shares)
        {
            share.Delete();
        }

        mDisplay.appendToResults("Share deleted");
        result = true;
    }
    catch (Exception ex)
    {
        mDisplay.showError("FAILED to remove share: " + ex.Message.ToString());
    }

    return result;
}

似乎您需要定义如何将文件复制到远程计算机,然后才能编写自动化的应用程序。对于超级用户来说,这可能是一个更大的问题。您将如何复制文件在远程计算机上?通过公开文件共享、FTP服务器或其他为文件传输而设计的服务。如果无处放置文件,则无法将文件复制到远程计算机上。为什么不在PC上创建共享?