Hi!
I want transfer file between two computers by IP. How I can do it? Below code working between local disc.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CopyLargeFile
{
public partial class Form1 : Form
{
BackgroundWorker worker = new BackgroundWorker();
public Form1()
{
InitializeComponent();
worker.WorkerSupportsCancellation = true;
worker.WorkerReportsProgress = true;
worker.ProgressChanged += Worker_ProgresChanged;
worker.DoWork += Worker_Dowork;
}
void CopyFile(string source, string des)
{
FileStream fsOut = new FileStream(des, FileMode.Create);
FileStream fsIn = new FileStream(source, FileMode.Open);
byte[] bt = new byte[1048756];
int readByte;
while((readByte = fsIn.Read(bt, 0, bt.Length)) > 0)
{
fsOut.Write(bt, 0, readByte);
worker.ReportProgress((int)(fsIn.Position * 100 / fsIn.Length));
}
fsIn.Close();
fsOut.Close();
}
private void Worker_Dowork(object sender, DoWorkEventArgs e)
{
CopyFile(txtSource.Text, txtTarget.Text);
}
private void Worker_ProgresChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
label3.Text = progressBar1.Value.ToString() + "%";
if(progressBar1.Value == 100)
{
progressBar1.Value = 0;
MessageBox.Show("Copy complete!");
}
}
private void btnSource_Click(object sender, EventArgs e)
{
OpenFileDialog o = new OpenFileDialog();
if(o.ShowDialog() == DialogResult.OK)
{
txtSource.Text = o.FileName;
}
}
private void btnTarget_Click(object sender, EventArgs e)
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
if (fbd.ShowDialog() == DialogResult.OK)
{
txtTarget.Text = Path.Combine(fbd.SelectedPath, Path.GetFileName(txtSource.Text));
}
}
private void btnCopy_Click(object sender, EventArgs e)
{
worker.RunWorkerAsync();
}
}
}