将文件从系统复制到网络(LAN)内的另一个系统 [英] copy a file from a system to another system inside a network(LAN)
本文介绍了将文件从系统复制到网络(LAN)内的另一个系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要开发Windows服务,该服务会将其家庭系统中的文件复制到网络内的另一个系统
以下是在家庭系统中读取文件的代码
i need to develop a Windows service which will copy a file in its home system to another system inside a network
below is the code to read the file in the home system
string ipAddress = "192.168.1.15";
//int port = int.Parse(txtHost.Text);
//string fileName = "Alert.wav";
if (!string.IsNullOrEmpty(ipAddress))
{
byte[] fileNameByte = Encoding.ASCII.GetBytes(fileName);
byte[] fileData = File.ReadAllBytes(fileName);
byte[] clientData = new byte[4 + fileNameByte.Length + fileData.Length];
byte[] fileNameLen = BitConverter.GetBytes(fileNameByte.Length);
fileNameLen.CopyTo(clientData, 0);
fileNameByte.CopyTo(clientData, 4);
fileData.CopyTo(clientData, 4 + fileNameByte.Length);
TcpClient clientSocket = new TcpClient(ipAddress,53);
NetworkStream networkStream = clientSocket.GetStream();
networkStream.Write(clientData, 0, clientData.GetLength(0));
networkStream.Close();
}
我需要将这些复制/写入的字节&然后将其写到网络内的另一台计算机中
i need to take these copied / written bytes & then write these in another computer inside a network
推荐答案
File.Copy(@"\\server\sourceFileFolder\file1", @"\\server2\destinationFileFolder\file1");
您需要复制文件的权限.如果出现错误,请尝试这种模拟.
You need a permission to copy file. if error comes try this impersonation.
AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
WindowsIdentity idnt = new WindowsIdentity(username, password);
WindowsImpersonationContext context = idnt.Impersonate();
File.Copy("Location1", "Location2"), true);
context.Undo();
或
or
using ( new Impersonator( "myUsername", "myDomainname", "myPassword" ) )
{
// code that executes under the new context.
File.Copy( x, y );
}
这篇关于将文件从系统复制到网络(LAN)内的另一个系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文