wpf檔案複製顯示進度條效果

來源:互聯網
上載者:User
也有一些介紹,先看運行效果
xaml如下:
xaml代碼
<window x:class="wpfthreadtest.mainwindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
title="mainwindow" closed="window_closed" loaded="window_loaded" height="209" width="537" resizemode="noresize">
<grid height="186">
<grid.columndefinitions>
<columndefinition width="132*" />
<columndefinition width="236*" />
<columndefinition width="147*" />
</grid.columndefinitions>
<grid.rowdefinitions>
<rowdefinition height="auto" />
<rowdefinition height="auto" />
<rowdefinition height="auto" />
<rowdefinition height="auto" />
<rowdefinition height="auto" />
</grid.rowdefinitions>
<textblock name="textblock1" text="目前時間" margin="35 10" height="auto" width="auto" />
<textbox grid.column="1" name="displaytimebythread" height="auto" margin="0 10" />
<textblock grid.row="1" name="textblock2" margin="35 10 0 10 " text="源檔案位置" />
<textbox grid.column="1" grid.row="1" name="srcfile" margin="0 10" />
<textbox grid.column="1" grid.row="2" name="savefilepath" margin="0 10" />
<textblock grid.row="2" name="textblock3" text="目標檔案位置" margin="35 10 0 10" />
<button content="..." grid.column="2" grid.row="1" name="button1" margin="10,10,66,10" click="button1_click" />
<button content="..." grid.column="2" grid.row="2" name="button2" margin="10,10,66,10" height="23" click="button2_click"/>
<button content="開始時間線程" grid.column="2" name="button3" margin="10,10,35,10" height="23" click="button3_click" />
<button content="開始檔案copy線程" grid.column="2" grid.row="3" height="29" horizontalalignment="left" name="button4" verticalalignment="top" width="119" click="button4_click" />
<textblock grid.row="3" name="copyflag" text="開始複製" />
<textblock name="displaycopyinfo" text="檔案copy進行中" grid.row="3" grid.column="1" />
<progressbar grid.column="1" grid.row="4" margin="0 2" height="8" name="copyprogress" />
</grid>
</window>
後台cs

 

後台cs內容
1 namespace wpfthreadtest
2 {
3 /// <summary>
4 /// mainwindow.xaml 的互動邏輯
5 /// </summary>
6   public partial class mainwindow : window
7 {
8 thread timethread;
9 thread copythread;
10 public mainwindow()
11 {
12 initializecomponent();
13 this.displaytimebythread.text = datetime.now.tolocaltime().tostring("yyyy年mm月dd日 hh:mm:ss"); ;
14 timethread = new thread(new threadstart(dispatcherthread));
15 }
16 private void button3_click(object sender, routedeventargs e)
17 {
18 timethread.start();
19 }
20 public void dispatcherthread()
21 {
22 //可以通過迴圈條件來控制ui的更新
23   while (true)
24 {
25 ///線程優先順序,最長逾時時間,方法委託(無參方法)
26   displaytimebythread.dispatcher.begininvoke(
27 dispatcherpriority.normal, new action(updatetime));
28 thread.sleep(1000);
29 }
30 }
31
32
33 private void updatetime()
34 {
35 this.displaytimebythread.text = datetime.now.tolocaltime().tostring("yyyy年mm月dd日 hh:mm:ss");
36 }
37
38 private void window_closed(object sender, eventargs e)
39 {
40 ///關閉所有啟動的線程
41 timethread.abort();
42 copythread.abort();
43 application.current.shutdown();
44 }
45
46 private void button1_click(object sender, routedeventargs e)
47 {
48 ///設定要複製的檔案全路徑
49 openfiledialog openfile = new openfiledialog();
50 openfile.addextension = true;
51 openfile.checkpathexists = true;
52 openfile.filter = "*.rar|*.rar|all files|*.*";
53 openfile.filterindex = 0;
54 openfile.multiselect = false;
55 bool? f=openfile.showdialog();
56 if (f!=null && f.value)
57 {
58 this.srcfile.text = openfile.filename;
59 }
60 }
61
62 private void button2_click(object sender, routedeventargs e)
63 {
64 ///設定目標檔案全路徑
65 savefiledialog savefile = new savefiledialog();
66 savefile.addextension = true;
67 savefile.filter = "*.rar|*.rar|all files|*.*";
68 savefile.filterindex = 0;
69
70 bool? f= savefile.showdialog();
71 if (f != null && f.value)
72 {
73 this.savefilepath.text = savefile.filename;
74 }
75 }
76
77 private void button4_click(object sender, routedeventargs e)
78 {
79 string filename=this.srcfile.text.trim();
80 string destpath=this.savefilepath.text.trim();
81 if(!file.exists(filename))
82 {
83 messagebox.show("源檔案不存在");
84 return;
85 }
86
87 ///copy file and nodify ui that rate of progress of file copy
88 this.copyflag.text = "開始複製。。。";
89
90 //設定進度條最大值,這句代碼寫的有點鬱悶
91 this.copyprogress.maximum = (new fileinfo(filename)).length;
92
93 //儲存複製檔案資訊,以進行傳遞
94 copyfileinfo c = new copyfileinfo() { sourcepath = filename, destpath = destpath };
95 //線程非同步呼叫複製檔案
96 copythread = new thread(new parameterizedthreadstart(copyfile));
97 copythread.start(c);
98
99 this.copyflag.text = "複製完成。。。";
100
101
102 }
103 /// <summary>
104 /// 複製檔案的委託方法
105 /// </summary>
106 /// <param name="obj">複製檔案的資訊</param>
107 private void copyfile(object obj)
108 {
109 copyfileinfo c = obj as copyfileinfo;
110 copyfile(c.sourcepath, c.destpath);
111 }
112 /// <summary>
113 /// 複製檔案
114 /// </summary>
115 /// <param name="sourcepath"></param>
116 /// <param name="destpath"></param>
117 private void copyfile( string sourcepath,string destpath)
118 {
119 fileinfo f = new fileinfo(sourcepath);
120 filestream fsr = f.openread();
121 filestream fsw = file.create(destpath);
122 long filelength = f.length;
123 byte[] buffer = new byte[1024];
124 int n = 0;
125
126 while (true)
127 {
128 ///設定線程優先順序
129 ///非同步呼叫updatecopyprogress方法
130 ///並傳遞2個long型別參數filelength 與 fsr.position
131 this.displaycopyinfo.dispatcher.begininvoke(dispatcherpriority.systemidle,
132 new action<long, long>(updatecopyprogress), filelength, fsr.position);
133
134 //讀寫檔案
135 n=fsr.read(buffer, 0, 1024);
136 if (n==0)
137 {
138 break;
139 }
140 fsw.write(buffer, 0, n);
141 fsw.flush();
142 thread.sleep(1);
143 }
144 fsr.close();
145 fsw.close();
146 }
147
148 private void updatecopyprogress(long filelength,long currentlength)
149 {
150 this.displaycopyinfo.text = string.format("總大小:{0},已複製:{1}", filelength, currentlength);
151 //重新整理進度條
152 this.copyprogress.value = currentlength;
153 }
154
155 private void window_loaded(object sender, routedeventargs e)
156 {
157
158 }
159
160 }
161 public class copyfileinfo
162 {
163 public string sourcepath { get; set; }
164 public string destpath { get; set; }
165 }
166 }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.