日期:2014-05-18  浏览次数:20868 次

二进制文件读取进度
C# code

string fileName=fileNamePath.Substring(fileNamePath.LastIndexOf("\\")+1);
            string NewFileName = DateTime.Now.ToString("yyMMddhhmmss") + DateTime.Now.Millisecond.ToString()+fileNamePath.Substring(fileNamePath.LastIndexOf("."));
            string fileNameExt = fileNamePath.Substring(fileNamePath.LastIndexOf(".") + 1);
            if(uriString.EndsWith("/")==false)
                uriString +="/";
            uriString = uriString + NewFileName;
            WebClient myWebCleint = new WebClient();
            myWebCleint.Credentials = CredentialCache.DefaultCredentials;//获取系统凭据
            //要上传的文件
            FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
            BinaryReader r = new BinaryReader(fs);
            
            try
            {
                byte[] postArray =r.ReadBytes((int)fs.Length);                
                Stream postStream = myWebCleint.OpenWrite(uriString, "PUT");//写文件
                progressBar1.Minimum = 0;
                progressBar1.Maximum = postArray.Length;

                
                if (postStream.CanWrite)
                {

                    
                    progressBar1.Value = r.Read(postArray, 0, postArray.Length);
                    [color=#FF0000]postStream.Write(postArray, 0, postArray.Length);[/color]                    MessageBox.Show("文件上传成功!");
                }
                else
                {
                    MessageBox.Show("文件目前不可写");
                }
                postStream.Close();
            }
            catch
            {
                MessageBox.Show("文件上传失败,请重试");
            }


以上是我写的利用二进制读取和写入文件,在文中红色字体那块,怎么获得文件已经读取多少了

------解决方案--------------------
stream.Position
------解决方案--------------------
progressBar1.Value = r.Read(postArray, 0, postArray.Length);
你是知道read了多少的,假如就是postArray.Length,那就直接在进度条上增加就是了
progressBar1.Value += postArray.Length