Click here to Skip to main content
15,901,505 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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

C#
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
Posted
Updated 28-Oct-12 21:09pm
v2

1 solution

C#
File.Copy(@"\\server\sourceFileFolder\file1", @"\\server2\destinationFileFolder\file1");


You need a permission to copy file. if error comes try this impersonation.


C#
AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);

WindowsIdentity idnt = new WindowsIdentity(username, password);

WindowsImpersonationContext context = idnt.Impersonate();

File.Copy("Location1", "Location2"), true);

context.Undo();


or
C#
using ( new Impersonator( "myUsername", "myDomainname", "myPassword" ) )
{
   // code that executes under the new context.
   File.Copy( x, y );
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900