Windows复制文件到远程共享目录

本文介绍如何在不使用Windows用户界面的情况下,通过批处理脚本实现文件的远程复制。主要内容包括使用net use命令连接到远程主机并验证身份,查看共享状态,断开共享连接,以及具体的命令示例。

When you are writing batch scripts (programs that run in the background without user intervention) you often need to copy a file on to a remote machine. If you are not already connected to the machine you will get this message when you try to initiate the copy:

C:\>copy file.txt \\remote-host\c$
Logon failure: unknown user name or bad password.
0 file(s) copied.

You have tried to access a share that is password protected. You must connect to the share and authenticate yourself. It’s easy to do this viaWindows Exploder->Tools->Map Network Drive… but how do you do it without using the Windows user interface?

As with all Windows commands it is overly complicated and not very simple to remember, so here are a couple of cheat sheets to connect to, view and disconnect from windows shares.

Connect to a share

net use z: /user:domain\user \\host\share password

See the status of a share

net user z:

Disconnect from a share

net use z: /delete

If you don’t specify a drive letter to map the share on to then you can only reference the share by its full name (\\host\share). You must also disconnect using the full path as well, for example:

net use \\host\share /delete

So to copy a file to a remote machine you can use the following lines:

net use z: /user:domain\user \\host\share password
copy my-file.txt z:\their-file.txt
net use z: /delete

or

net use /user:domain\user \\host\share password
copy my-file.txt \\host\share\their-file.txt
net use \\host\share /delete

depending on whatever floats your boat! Well ok, it’s not that complicated.

在C#中实现将文件复制到远程共享目录的功能,可以通过多种方式完成。以下是一些常用的方法和实现思路: ### 使用Windows网络共享的方式 如果目标远程共享目录是基于Windows的网络共享(如SMB协议),可以使用`System.IO`命名空间中的类来实现文件复制。前提是本地程序有权限访问该共享目录。 ```csharp using System; using System.IO; class Program { static void Main() { string sourcePath = @"C:\LocalFolder\file.txt"; string destinationPath = @"\\RemoteServer\SharedFolder\file.txt"; try { File.Copy(sourcePath, destinationPath, true); Console.WriteLine("文件复制成功。"); } catch (Exception ex) { Console.WriteLine("复制文件时出错:" + ex.Message); } } } ``` 如果远程共享目录需要特定的用户名和密码访问,可以通过`WindowsIdentity.Impersonate`实现模拟特定用户身份,从而获得访问权限。 ### 使用SFTP协议上传文件 如果远程共享目录是基于Linux系统,通常会使用SFTP协议进行文件传输。可以使用`Renci.SshNet`库来实现SFTP文件传输功能。以下是一个示例代码: ```csharp using System; using System.IO; using Renci.SshNet; class Program { static void Main() { string host = "192.168.1.40"; string username = "root"; string password = "123456"; string localFilePath = Path.Combine(Environment.CurrentDirectory, "111.txt"); string remoteFilePath = "/home/shared/黑龙江,哈尔滨市,阿城区县在线资源目录/采伐数据/采伐参考_20200722142705.zdb"; using (var sftp = new SftpClient(host, username, password)) { try { sftp.Connect(); // 上传文件 using (var fileStream = new FileStream(localFilePath, FileMode.Open)) { sftp.UploadFile(fileStream, remoteFilePath); } Console.WriteLine("文件上传成功。"); } catch (Exception ex) { Console.WriteLine("文件传输失败:" + ex.Message); } finally { sftp.Disconnect(); } } } } ``` ### 注意事项 1. **权限问题**:确保程序有权限访问远程共享目录。如果使用SFTP,需要确保SSH服务在目标服务器上运行,并且提供了正确的用户名和密码。 2. **路径问题**:远程路径中的特殊字符(如中文或逗号)可能导致问题,建议对路径进行适当的编码处理。 3. **异常处理**:在实际开发中,务必加入完善的异常处理机制,以应对网络中断、权限不足等问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值