winform复制文件带进度条和制作安装包

最近做了一个winform程序,遇到程序更新的问题。【升级包】:简单的文件复制。此功能将程序中指定的文件复制到安装目录下达到升级程序,即简单的文件COPY,在网上有很多在线升级的功能。思路很清晰,但是内容有点点多,由于本人水平低,就用了一个比较笨的办法。

贴上源代码:

ContractedBlock.gif ExpandedBlockStart.gif Code
  1using System;
  2using System.Collections.Generic;
  3using System.ComponentModel;
  4using System.Data;
  5using System.Drawing;
  6using System.Linq;
  7using System.Text;
  8using System.Windows.Forms;
  9using System.Collections;
 10using System.Threading;
 11using System.IO;
 12
 13namespace WindowsFormsApplication1
 14ExpandedBlockStart.gifContractedBlock.gif{
 15    public partial class UpdaterFrm : Form
 16ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 17        public delegate void DelegateProgressBarAdd();
 18        public DelegateProgressBarAdd m_DelegateProgressBarAdd;
 19        private string targetPath = "";
 20        private string fileName = "";
 21        public UpdaterFrm()
 22ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 23            InitializeComponent();
 24            m_DelegateProgressBarAdd = new DelegateProgressBarAdd(this.ProgressBarAdd);
 25
 26        }

 27        private void UpdaterFrm_Load(object sender, EventArgs e)
 28ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 29
 30        }

 31
 32        private void addFile(string directory, string targetPath)
 33ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 34            try
 35ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 36                ArrayList arraylist = new ArrayList();
 37                int num = Directory.GetFiles(directory).Length;
 38                this.progressBar1.Maximum = num;
 39                string name = "";
 40                string folder = "";
 41                string temp = targetPath;
 42                foreach (string fi in Directory.GetFiles(directory))
 43ExpandedSubBlockStart.gifContractedSubBlock.gif                {
 44                    targetPath = temp;
 45                    string filetype = fi.Substring(fi.LastIndexOf("."+ 1);
 46                    string filename = fi.Substring(fi.LastIndexOf("\\"+ 1);
 47
 48                    if (File.Exists(targetPath + "\\" + filename.Trim()))
 49ExpandedSubBlockStart.gifContractedSubBlock.gif                    {
 50
 51                        File.Delete(targetPath + "\\" + filename.Trim());//刪除文件
 52                    }

 53                    File.Copy(fi, targetPath + "\\" + filename.Trim());
 54
 55ExpandedSubBlockStart.gifContractedSubBlock.gif                    /**/////File.Move(@"d:\" + filename.Trim(), @"e:\" + filename.Trim());//移動文件  }  
 56
 57                    foreach (string di in Directory.GetDirectories(directory))
 58ExpandedSubBlockStart.gifContractedSubBlock.gif                    {
 59                        name = di.Substring(di.LastIndexOf("\\"), di.Length - di.LastIndexOf("\\"));
 60                        folder = name.Substring(1, name.Length - 1);
 61                        targetPath = targetPath + name;
 62
 63                        //文件夹中还在文件夹则创建子目录
 64                        if (!Directory.Exists(targetPath))
 65ExpandedSubBlockStart.gifContractedSubBlock.gif                        {
 66                            Directory.CreateDirectory(targetPath);
 67                        }

 68
 69                        //COPY文件
 70                        addFile(di, targetPath);
 71                    }

 72                    if (progressBar1.Value < progressBar1.Maximum)
 73ExpandedSubBlockStart.gifContractedSubBlock.gif                    {
 74
 75                        fileName = filename;
 76                        this.Invoke(m_DelegateProgressBarAdd, null);
 77                        Thread.Sleep(500);
 78                        Application.DoEvents();
 79                    }

 80
 81                }

 82                this.btnUpdate.Text = "更新成功!";
 83                this.btnUpdate.Enabled = false;
 84                this.label2.Text = "更新完成";
 85            }

 86            catch(Exception ex)
 87ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 88                MessageBox.Show("更新失败!","提示");
 89            }

 90              
 91     }

 92
 93        private void ProgressBarAdd() 
 94ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 95            this.progressBar1.Value++;
 96            this.label2.Text = "正在更新:" + fileName + " 已完成" + string.Format("{0:p}", (double)this.progressBar1.Value / this.progressBar1.Maximum);
 97        }

 98
 99
100        private void btnView_Click(object sender, EventArgs e)
101ExpandedSubBlockStart.gifContractedSubBlock.gif        {
102            //选择安装目录
103             FolderBrowserDialog fbd = new FolderBrowserDialog();
104            fbd.ShowDialog();
105            targetPath = fbd.SelectedPath; //获得选择的文件夹路径   
106            this.txtPath.Text = targetPath;
107           
108        }

109
110        private void btnExit_Click(object sender, EventArgs e)
111ExpandedSubBlockStart.gifContractedSubBlock.gif        {
112            this.Close();
113        }

114
115        private void btnUpdate_Click(object sender, EventArgs e)
116ExpandedSubBlockStart.gifContractedSubBlock.gif        {
117            //读取指定文件
118            const string FILENAME = "UpdateFiles";
119            string directory = System.Windows.Forms.Application.StartupPath + "\\" + FILENAME;
120            targetPath = this.txtPath.Text.Trim();
121            
122
123            if (targetPath != "")
124ExpandedSubBlockStart.gifContractedSubBlock.gif            {
125                if (Directory.Exists(targetPath))
126ExpandedSubBlockStart.gifContractedSubBlock.gif                {
127                  addFile(directory, targetPath);
128                }

129                else
130ExpandedSubBlockStart.gifContractedSubBlock.gif                {
131                    MessageBox.Show("目录不存在,请重新选择!""提示:");
132                    return;
133                }

134            }

135            else
136ExpandedSubBlockStart.gifContractedSubBlock.gif            {
137                MessageBox.Show("请选择安装目录!","提示:");
138                return;
139            }

140        }

141
142       
143
144     }

145    
146}

147

 文件COPY的核心代码:

addFile(string directory, string targetPath)中,其中要注意的是:进度条,需要用异步处理,通过事件委托执行,

 Application.DoEvents();【注释:处理当前在消息队列中所有的Window消息】这句代码是关键,如果无,则,再输出提示语句【this.label2.Text = "正在更新:" + fileName + " 已完成" + string.Format("{0:p}", (double)this.progressBar1.Value / this.progressBar1.Maximum);】的时候,虽然进度条一直再变化,但是label2.text 的值,一直到进度条执行完了,才显示。


 

源码下载:

/Files/yingpp/Updater.rar

 

源码中有还有个安装包的制作:

下面讲安装包中应该注意的事项:

1,在解决方案下添加新项,选择:安装项目【安装向导也可以的】

如图:

 

 

 2,选中新建安装项目,点右键,添加 项目输出:

如图:

添加 主输出、本地化资源、内容文件等即可!

3,再次选择安装项目,右键,视图,-->文件系统

可以创建文件夹,上传文件,

创建桌面快捷方式:

用户桌面-->添加-->项目输出

然后 选择刚才添加的项目主输出,右键,创建快捷方式,设置属性即可

注意:WorkingFolder 设置为 应用程序文件夹

如图:

 

设置安装项目的属性:

RemovePreviousVersions设置为 True,在安装的时候,会先删除以前的版本。

根据以上步骤,编译生成。然后安装 即完成安装包的制作。

 

转载于:https://www.cnblogs.com/yingpp/archive/2009/02/10/1387429.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在WinForm中实现圆形进度条,可以使用ProgressBar控件,并通过自定义绘制来实现。 首先,在WinForm的设计界面上添加一个ProgressBar控件,并设置其Style为Continuous,以实现平滑的动画效果。 接下来,在Form的Load事件中添加以下代码: ```csharp private void Form1_Load(object sender, EventArgs e) { progressBar1.Maximum = 100; // 设置进度条的最大值 progressBar1.Minimum = 0; // 设置进度条的最小值 progressBar1.Step = 1; // 设置进度条每次增加的步长 // 设置进度条的样式为自定义 progressBar1.SetStyle(ControlStyles.UserPaint, true); progressBar1.SetStyle(ControlStyles.OptimizedDoubleBuffer, true); progressBar1.SetStyle(ControlStyles.AllPaintingInWmPaint, true); progressBar1.SetStyle(ControlStyles.SupportsTransparentBackColor, true); } ``` 然后,对ProgressBar的绘制事件进行自定义绘制,以实现圆形进度条的效果。在Form中添加以下代码: ```csharp private void progressBar1_Paint(object sender, PaintEventArgs e) { using (Graphics graphics = e.Graphics) { graphics.SmoothingMode = SmoothingMode.AntiAlias; graphics.Clear(progressBar1.BackColor); float angle = 360 * progressBar1.Value / (progressBar1.Maximum - progressBar1.Minimum); using (SolidBrush brush = new SolidBrush(progressBar1.ForeColor)) { graphics.FillPie(brush, new Rectangle(0, 0, progressBar1.Width - 1, progressBar1.Height - 1), -90, angle); } } } ``` 最后,通过调用ProgressBar的PerformStep方法来动态更新进度条的进度,即可实现圆形进度条的效果。例如,在按钮的Click事件中添加以下代码: ```csharp private void button1_Click(object sender, EventArgs e) { if (progressBar1.Value < progressBar1.Maximum) { progressBar1.PerformStep(); } } ``` 通过以上步骤,就可以在WinForm中实现圆形进度条的效果了。 ### 回答2: 在WinForm中实现圆形进度条可以通过自定义控件来实现。 首先,创建一个继承自Control类的自定义控件CircleProgressBar,然后在该类中重写OnPaint方法来绘制圆形进度条。 在OnPaint方法中,我们可以使用Graphics类的一些方法和属性来绘制圆形进度条的背景和进度。具体实现步骤如下: 1. 创建一个Graphics对象,用于绘制进度条。 2. 设置Graphics对象的SmoothingMode属性为AntiAlias,以获得平滑的绘制效果。 3. 根据控件的宽度和高度计算出圆形的半径。 4. 创建一个矩形,作为进度条的外框。 5. 使用Graphics对象的DrawEllipse方法绘制圆形的外框。 6. 设置Graphics对象的Clip属性为矩形,以便限制进度条的绘制范围。 7. 计算出进度条的角度,根据进度值和总进度值的比例计算。 8. 使用Graphics对象的DrawArc方法绘制进度条的弧度。 9. 调用Graphics对象的Dispose方法释放资源。 在使用CircleProgressBar控件时,只需将其添加到窗体中,并设置进度值和总进度值即可。 示例代码如下: ```csharp using System; using System.Drawing; using System.Windows.Forms; public class CircleProgressBar : Control { private int progress; private int total; public CircleProgressBar() { progress = 0; total = 100; } public int Progress { get { return progress; } set { progress = value; if (progress < 0) progress = 0; if (progress > total) progress = total; Invalidate(); // 重绘控件 } } public int Total { get { return total; } set { total = value; if (total <= 0) total = 1; if (progress > total) progress = total; Invalidate(); // 重绘控件 } } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); Graphics g = e.Graphics; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; int diameter = Math.Min(Width, Height); // 获取圆形的直径 Rectangle rect = new Rectangle(0, 0, diameter, diameter); g.DrawEllipse(Pens.Black, rect); // 绘制圆形的外框 using (GraphicsPath path = new GraphicsPath()) using (Pen pen = new Pen(Color.Blue, 5)) { path.AddArc(rect, -90, (float)(progress * 360 / total)); // 计算进度条的弧度 g.Clip = new Region(rect); // 设置绘制范围 g.DrawPath(pen, path); // 绘制进度条的弧度 } g.Dispose(); } } ``` 通过以上步骤,我们就可以自定义一个圆形进度条WinForm控件,并在窗体中使用它来展示圆形的进度。 ### 回答3: 在WinForm中实现圆形进度条,可以通过自定义控件来实现。以下是实现的步骤: 1. 创建一个新的自定义控件,继承自Panel或者UserControl,并命名为CircularProgressBar。 2. 在该自定义控件中,声明一个整型变量progressValue用于表示进度的值,以及一个整型变量maxValue表示进度的最大值。 3. 在构造函数中,设置控件的默认大小和背景颜色。 4. 重写OnPaint方法,在该方法中绘制圆形进度条的背景和进度条的进度。 5. 在OnPaint方法中,先绘制圆形背景,可以使用Graphics的DrawEllipse方法来绘制一个圆形。 6. 根据当前的进度值和最大值,计算出进度条的角度,通过遍历的方式,使用Graphics的DrawArc方法来绘制进度条。 7. 在控件中新增一个SetProgress方法,用于设置进度条的进度值,并在该方法中调用Invalidate方法触发控件的重绘。 8. 在MainForm中使用该自定义控件,可以通过设置CircularProgressBar的Size和Location属性来调整控件的大小和位置。 使用以上的步骤,即可在WinForm中实现一个圆形进度条的自定义控件。控件的进度值可以通过SetProgress方法来动态设置,从而实现进度的更新和显示。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值