如何使用C#将文件从一台计算机发送到另一台计算机 [英] How to send a file from one computer to another computer using C#
本文介绍了如何使用C#将文件从一台计算机发送到另一台计算机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在编写代码,用于将一个文件从一台计算机传输到另一台计算机,在我的情况下,我能够在单个系统中传输文件客户端和服务器,但我无法在两个不同的系统之间进行。
请帮我解决这个问题,
提前致谢。
我尝试了什么:
服务器:
private void Form1_Load( object sender,EventArgs e)
{
// IPHostEntry IPHost = Dns.GetHostByName(Dns.GetHostName());
// lbl_Status.Text =我的IP地址是+ IPHost.AddressList [0] .ToString( );
nSockets = new ArrayList();
Thread thdListener = new 线程( new ThreadStart(ListenerThread));
thdListener.Start();
}
public void ListenerThread()
{
TcpListener tcpListener = new TcpListener(IPAddress.Parse(GetIp()), 8080 ) ;
tcpListener.Start();
while ( true )
{
Socket handlerSocket = tcpListener.AcceptSocket();
// TcpClient client = tcpListener.AcceptTcpClient();
// MessageBox.Show(client.Client.RemoteEndPoint +。);
if (handlerSocket.Connected)
{
lb_Connections.Invoke((MethodInvoker) delegate
{
// btn_SaveFile.PerformClick();
saveFileDialog_Server.ShowDialog( );
txt_SaveFile_Location.Text = saveFileDialog_Server.FileName;
lb_Connections.Items.Add(handlerSocket.RemoteEndPoint.ToString()+ 已连接。);
});
lock ( this )
{
nSockets 。新增(HandlerSocket的);
}
ThreadStart thdstHandler = new ThreadStart(handlerThread);
Thread thdHandler = new Thread(thdstHandler);
thdHandler.Start();
}
}
}
public void handlerThread ()
{
套接字handlerSocket =(套接字)nSockets [nSockets.Count - 1 ];
NetworkStream networkStream = new NetworkStream(handlerSocket);
int thisRead = 0 ;
int blockSize = 1024 ;
字节 [] dataByte = new 字节< /跨度> [BLOCKSIZE];
lock (此)
{
// 只有一个流程可以访问
// 任何给定时间的同一文件
流fileStream = File.OpenWrite(txt_SaveFile_Location.Text);
while ( true )
{
thisRead = networkStream.Read (dataByte, 0 ,blockSize);
fileStream.Write(dataByte, 0 ,thisRead);
if (thisRead == 0 ) break 跨度>;
}
fileStream.Close();
}
lb_Connections.Invoke((MethodInvoker)委托
{
lb_Connections.Items.Add( 写入文件);
});
handlerSocket = null ;
}
public string GetIp()
{
string name = Dns.GetHostName();
IPHostEntry entry = Dns.GetHostEntry(name);
IPAddress [] addr = entry.AddressList;
if (addr [ 1 ]。ToString()。Split(' 。')。长度== 4 )
{
return addr [ 1 ]。ToString();
}
return addr [ 2 ]。ToString();
}
客户:
private void button1_Click( object sender,EventArgs e)
{
DialogResult dialogResult = folderBrowserDialog_openFiles.ShowDialog();
txt_OpenFiles.Text = folderBrowserDialog_openFiles.SelectedPath;
filepath = txt_OpenFiles.Text;
}
private void button2_Click( object sender,EventArgs e)
{
try
{
saveFileDialog_zip。 Filter = @ Zip文件(.zip)| * .zip |所有文件(*。*)| *。* 跨度>;
DialogResult dr = saveFileDialog_zip.ShowDialog();
saveFile = saveFileDialog_zip.FileName;
fileName = saveFile;
ZipFile.CreateFromDirectory(filepath,fileName);
MessageBox.Show( 在保存位置创建的Zip文件);
}
catch (异常异常)
{
MessageBox.Show( 未创建Zip文件);
}
}
private void btn_Browse_Zip_Click( object sender,EventArgs e)
{
DialogResult drbrowse_zip = openFileDialog_OpenZip.ShowDialog();
openFileDialog_OpenZip.Filter = Zip文件(.zip)| * .zip |所有文件(*。* )| *。*;
openFileDialog_OpenZip.FilterIndex = 1 ;
if (drbrowse_zip == System.Windows.Forms.DialogResult.OK)
{
txt_BrowseZip.Text = openFileDialog_OpenZip.FileName;
}
}
private void btn_Transfer_Click( object sender,EventArgs e)
{
Stream fileStream = File.OpenRead(txt_BrowseZip.Text);
// 为文件分配内存空间
byte [] fileBuffer = new byte [fileStream.Length];
fileStream.Read(fileBuffer, 0 ,( int )fileStream.Length);
// 打开TCP / IP连接并发送数据
TcpClient clientSocket = new TcpClient(txt_Server.Text, 8080 );
NetworkStream networkStream = clientSocket.GetStream();
networkStream.Write(fileBuffer, 0 ,fileBuffer.GetLength( 0 ));
networkStream.Close();
}
解决方案
你可以尝试使用ftp
使用C#.Net 2.0的简单FTP演示应用程序 [ ^ 一>
i am writing code for to transfer one file from one computer to another computer, in my case i am able to transfer file client and server in single system,but i am not able do for between two different systems.
Please help me to resolve this issue,
Thanks in advance.
What I have tried:
Server:
private void Form1_Load(object sender, EventArgs e)
{
//IPHostEntry IPHost = Dns.GetHostByName(Dns.GetHostName());
//lbl_Status.Text = "My IP address is " + IPHost.AddressList[0].ToString();
nSockets = new ArrayList();
Thread thdListener = new Thread(new ThreadStart(ListenerThread));
thdListener.Start();
}
public void ListenerThread()
{
TcpListener tcpListener = new TcpListener(IPAddress.Parse(GetIp()),8080);
tcpListener.Start();
while (true)
{
Socket handlerSocket = tcpListener.AcceptSocket();
//TcpClient client = tcpListener.AcceptTcpClient();
//MessageBox.Show(client.Client.RemoteEndPoint + ".");
if (handlerSocket.Connected)
{
lb_Connections.Invoke((MethodInvoker)delegate
{
//btn_SaveFile.PerformClick();
saveFileDialog_Server.ShowDialog();
txt_SaveFile_Location.Text = saveFileDialog_Server.FileName;
lb_Connections.Items.Add(handlerSocket.RemoteEndPoint.ToString() + " connected.");
});
lock (this)
{
nSockets.Add(handlerSocket);
}
ThreadStart thdstHandler = new ThreadStart(handlerThread);
Thread thdHandler = new Thread(thdstHandler);
thdHandler.Start();
}
}
}
public void handlerThread()
{
Socket handlerSocket = (Socket)nSockets[nSockets.Count - 1];
NetworkStream networkStream = new NetworkStream(handlerSocket);
int thisRead = 0;
int blockSize = 1024;
Byte[] dataByte = new Byte[blockSize];
lock (this)
{
// Only one process can access
// the same file at any given time
Stream fileStream = File.OpenWrite(txt_SaveFile_Location.Text);
while (true)
{
thisRead = networkStream.Read(dataByte, 0, blockSize);
fileStream.Write(dataByte, 0, thisRead);
if (thisRead == 0) break;
}
fileStream.Close();
}
lb_Connections.Invoke((MethodInvoker)delegate
{
lb_Connections.Items.Add("File Written");
});
handlerSocket = null;
}
public string GetIp()
{
string name = Dns.GetHostName();
IPHostEntry entry = Dns.GetHostEntry(name);
IPAddress[] addr = entry.AddressList;
if (addr[1].ToString().Split('.').Length == 4)
{
return addr[1].ToString();
}
return addr[2].ToString();
}
Client:
private void button1_Click(object sender, EventArgs e)
{
DialogResult dialogResult =folderBrowserDialog_openFiles.ShowDialog();
txt_OpenFiles.Text = folderBrowserDialog_openFiles.SelectedPath;
filepath = txt_OpenFiles.Text;
}
private void button2_Click(object sender, EventArgs e)
{
try
{
saveFileDialog_zip.Filter = @"Zip Files (.zip) | *.zip | All Files(*.*) | *.*";
DialogResult dr = saveFileDialog_zip.ShowDialog();
saveFile = saveFileDialog_zip.FileName;
fileName = saveFile;
ZipFile.CreateFromDirectory(filepath, fileName);
MessageBox.Show("Zip File Created at your Saved Location");
}
catch (Exception exception)
{
MessageBox.Show("Zip file not created");
}
}
private void btn_Browse_Zip_Click(object sender, EventArgs e)
{
DialogResult drbrowse_zip=openFileDialog_OpenZip.ShowDialog();
openFileDialog_OpenZip.Filter = "Zip Files (.zip) | *.zip | All Files(*.*) | *.*";
openFileDialog_OpenZip.FilterIndex = 1;
if (drbrowse_zip == System.Windows.Forms.DialogResult.OK)
{
txt_BrowseZip.Text = openFileDialog_OpenZip.FileName;
}
}
private void btn_Transfer_Click(object sender, EventArgs e)
{
Stream fileStream = File.OpenRead(txt_BrowseZip.Text);
// Alocate memory space for the file
byte[] fileBuffer = new byte[fileStream.Length];
fileStream.Read(fileBuffer, 0, (int)fileStream.Length);
// Open a TCP/IP Connection and send the data
TcpClient clientSocket=new TcpClient(txt_Server.Text,8080);
NetworkStream networkStream = clientSocket.GetStream();
networkStream.Write(fileBuffer, 0, fileBuffer.GetLength(0));
networkStream.Close();
}
解决方案
You can try with ftp
Simple FTP demo application using C#.Net 2.0[^]
这篇关于如何使用C#将文件从一台计算机发送到另一台计算机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文