基本信息
源码名称:c#winform下载上传文件显示进度条.rar
源码大小:0.04M
文件格式:.rar
开发语言:C#
更新时间:2017-11-16
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍

c#winform下载上传文件显示进度条.rar

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog importDialog = new OpenFileDialog();
            //设置文件类型 
            //importDialog.Filter = "地块导入文件(*.xls)|*.xls";
            //设置默认文件类型显示顺序 
            importDialog.FilterIndex = 1;
            //保存对话框是否记忆上次打开的目录 
            importDialog.RestoreDirectory = true;
            //点了保存按钮进入 
            if (importDialog.ShowDialog() == DialogResult.OK)
            {
                string localFilePath = importDialog.FileName.ToString(); //获得文件路径 
                UpLoadFile(localFilePath, @"\\192.168.1.3\测试");
                //UpLoadFile(localFilePath, @"\\192.168.1.101\工具\测试");
                //System.Net.WebClient webclient = new System.Net.WebClient();
                //webclient.UploadFile(@"\\192.168.1.101\工具\测试", localFilePath); 
            }
        }


        //// <summary>
        /// WebClient上传文件至服务器
        /// </summary>
        /// <param name="fileNamePath">文件名,全路径格式</param>
        /// <param name="uriString">服务器文件夹路径</param>
        private void UpLoadFile(string fileNamePath, string uriString)
        {
            //文件名
            string FileName = Path.GetFileName(fileNamePath);
            //文件格式
            string fileNameExt = fileNamePath.Substring(fileNamePath.LastIndexOf(".")   1);
            if (uriString.EndsWith("/") == false) uriString = uriString   "/";
            uriString = uriString   FileName;
            WebClient myWebClient = new WebClient();
            myWebClient.Credentials = CredentialCache.DefaultCredentials;
            // 要上传的文件
            FileStream fs = new FileStream(fileNamePath, FileMode.Open, FileAccess.Read);
            BinaryReader r = new BinaryReader(fs);
            byte[] postArray = r.ReadBytes((int)fs.Length);
            Stream postStream = myWebClient.OpenWrite(uriString, "PUT");
            if (postStream.CanWrite)
            {
                postStream.Write(postArray, 0, postArray.Length);
            }
            else
            {
                MessageBox.Show("文件目前不可写!");
            }
            postStream.Close();
            MessageBox.Show("文件上传成功");
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }
    }
}