文件传输内部网络 [英] File Transfer internal network

查看:64
本文介绍了文件传输内部网络的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
对于要处理的这类项目,我还很陌生.
我正在开发一个小项目,用于将文件从一台计算机传输到另一台计算机.
我知道如何使用File.Copy命令,但是这些计算机将不会始终位于同一工作组中,并且可能具有不同的用户名和密码(因此,共享并不总是直接的)
我尝试搜索但没有运气.
我也希望两台机器文件都显示在listview中.
我想保持它的简单性,我想一些类似UltraVNC的文件传输的操作.(1表格,拖放)
您的帮助/想法将不胜感激.
谢谢

Hi guys,
I am quite new to this type of project i am tackling.
I am developing a small project to transfer files from one machine to another.
I know how to use the File.Copy command, but these computers will not always be on the same workgroup, and might have different username''s & passwords(so the sharing is not always direct)
I have tried searching but with no luck.
I would also like both machines files to show in listview.
I want to keep it very simple, i have something in mind like UltraVNC''s file transfer.(1 form, drag & drop)
Your help/ideas will be appreciated.
Thanks

推荐答案

可以使用PowerShell访问远程计算机,但是您需要在远程计算机上使用正确的应用程序.您的另一个选择是在远程计算机上创建服务以与之通信,该服务将提供所需的信息.您可以从c#运行powershell.请参阅如何从C#运行PowerShell脚本 [ ^ ]

Powershell是一个很棒的工具,它可以运行dos和Unix命令(也许不是全部),并且可以处理对象而不是字符串.命令将返回一个包含infroamation的对象,因此执行目录时,不会返回字符串,而是包含详细信息的对象.
It is possible to access a remote machine with PowerShell, but you need the correct applicaiton on the remote machine. Your other option is to create a service on the remote machine to communicate with that will provide the required information. You can run powershell from c#. See How to run PowerShell scripts from C#[^]

Powershell is a great tool that can run dos and Unix commands (maybe not all), and works with objects instead of strings. Commands will return an object that contains the infroamtion, so when you do a dir, you do not get strings back, but objects that contains the detailed information.


您甚至可以使用File.Copy甚至跨域(工作组).您只需要具有所需的凭据(用户名和密码).据我所知,在API中,仅.NET不需要直接实现.

我为您找到了以下代码:
You can use File.Copy even across domains (workgroups). You only need to have the needed credentials (username & password). As I know there is no direct implementation needed in .NET only, in the API.

I found this code for you:
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 result = WNetAddConnection2(
            netResource, 
            credentials.Password,
            credentials.UserName,
            0);

        if (result != 0)
        {
            throw new Win32Exception(result, "Error connecting to remote share");
        }   
    }

    ~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
}


它可以用于在文件传输过程中模拟他人.如您所见,您甚至可以模拟两个不同的帐户.


It can be used to impersonate somebody during the file transfer. As you can see, you even can impersonate two different accounts.

using (new NetworkConnection(@"\\server\read", readCredentials))
using (new NetworkConnection(@"\\server2\write", writeCredentials)) {
   File.Copy(@"\\server\read\file", @"\\server2\write\file");
}


当然,您可以使用管理员共享,不需要实际共享,但是您需要使用管理员帐户.您可以将不同的凭据存储在应用程序设置中(如果需要,可以加密),也可以在所有受影响的计算机上添加公用的用户名/密码对.

顺便说一句,您甚至可以使用wmi复制文件(如psexec一样),但是在我看来,这更加复杂.


Of course, you can use admin shares, no need for real shares, but you need admin account for that. You could store the different credentials in your application setting (encrypted, if needed), or you could add a common username-password pair on all affected machines.

By the way, you can even use wmi to copy files (as psexec does), but it seems to me more complicated.


对不起,已经出国了一段时间,使用了Filestream最后使用BinaryWriter,在新窗口中显示进度,感谢您的帮助.
Sorry, was out of the country for a while, used Filestream with a BinaryWriter in the end, displaying a progress in a new window, thanks for your help.


这篇关于文件传输内部网络的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆