您的位置:首页 > 其它

WPF 文件拷贝进度显示

2011-09-13 11:41 453 查看
public partial class MainWindow : Window
{

int totalSize;
int position;
const int BUFFER_SIZE = 1024*1024;
byte[] buffer;
Stream stream;

public MainWindow()
{
InitializeComponent();
}

string copypath = "";
private void btncopy_Click(object sender, RoutedEventArgs e)
{

System.Windows.Forms.FolderBrowserDialog dlg = new System.Windows.Forms.FolderBrowserDialog();
System.Windows.Interop.HwndSource source = PresentationSource.FromVisual(this) as System.Windows.Interop.HwndSource;
System.Windows.Forms.IWin32Window win = new OldWindow(source.Handle);
System.Windows.Forms.DialogResult result = dlg.ShowDialog(win);
//File.Copy("", dlg.SelectedPath + @"/" + "", true);
copypath = dlg.SelectedPath + @"/" + "拷贝文件.zip";

//string strFile = "";
//OpenFileDialog dlg = new OpenFileDialog();
//if (dlg.ShowDialog().ToString().Equals("OK"))
//{
//    strFile = dlg.FileName;
//}
//else
//{
//    return;
//}

FileStream fs = new FileStream(@"E:\Videos\传输测试文件.zip", FileMode.Open, FileAccess.Read);
totalSize = (int)fs.Length;
stream = fs;

//Delete file which aready exists.
//if (File.Exists("传输测试文件.zip"))
//{
//    File.Delete("传输测试文件.zip");
//}

//Copy file while larger than 4KB.
if (totalSize > BUFFER_SIZE)
{
buffer = new byte[BUFFER_SIZE];

// Async Invoke
stream.BeginRead(buffer, 0, BUFFER_SIZE, new AsyncCallback(AsyncCopyFile), null);
//MessageBox.Show("文件拷贝完毕!");
}
else
{
fs.Close();
}
}

private void AsyncCopyFile(IAsyncResult ar)
{
int readenLength;

//Lock FileStream
lock (stream)
{
// When stream endread, get readed length
readenLength = stream.EndRead(ar);
}

// Write to disk
FileStream fsWriter = new FileStream(copypath, FileMode.Append, FileAccess.Write);
fsWriter.Write(buffer, 0, buffer.Length);
fsWriter.Close();

// Current stream position
position += readenLength;

// Response UI
//MethodInvoker m = new MethodInvoker(SynchProgressBar);
//m.BeginInvoke(null, null);

Dispatcher.Invoke(new Action(SynchProgressBar));

if (position >= totalSize)  //read over
{
stream.Close();
return;
}

// Continue to read and write
lock (stream)
{
int leftSize = totalSize - position;

if (leftSize < BUFFER_SIZE)
{
buffer = new byte[leftSize];
}
stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(AsyncCopyFile), null);
}
}

private void SynchProgressBar()
{
this.progressBar1.Minimum = 0;
//this.progressBar1.Maximum = totalSize;
//this.progressBar1.Value = position;
SetControlPropertyValue(this.progressBar1, "Maximum", totalSize);
SetControlPropertyValue(this.progressBar1, "Value", position);
}

delegate void SetControlValueCallback(Control oControl, string propName, object propValue);
private void SetControlPropertyValue(Control oControl, string propName, object propValue)
{
if (System.Threading.Thread.CurrentThread!=oControl.Dispatcher.Thread)
{
SetControlValueCallback d = new SetControlValueCallback(SetControlPropertyValue);
oControl.Dispatcher.Invoke(d, new object[] { oControl, propName, propValue });
}
else
{
Type t = oControl.GetType();
PropertyInfo[] props = t.GetProperties();
foreach (PropertyInfo p in props)
{
if (p.Name.ToUpper() == propName.ToUpper())
{
p.SetValue(oControl, propValue, null);
}
}
}
}

private void btnexit_Click(object sender, RoutedEventArgs e)
{
Application.Current.MainWindow.Close();
}

private void Window_Unloaded(object sender, RoutedEventArgs e)
{
GC.Collect();
GC.WaitForPendingFinalizers();
}
}

public class OldWindow : System.Windows.Forms.IWin32Window
{
IntPtr _handle;
public OldWindow(IntPtr handle)
{
_handle = handle;
}
#region IWin32Window Members
IntPtr System.Windows.Forms.IWin32Window.Handle
{
get { return _handle; }
}
#endregion
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: