public ImageForm(string path) { this.Height = 700; this.Width = 800; InitializeComponent(); path = "\\" + path.Substring(1); if (File.Exists(path)) { var uri = new Uri(path); //strImagePath 就绝对路径 try { img.Source = new BitmapImage(uri); FormCommon.ShowForm("", this); } catch (Exception e) { if (e is NotSupportedException) { FormCommon.ShowErr("图片格式错误!"); } else { FormCommon.ShowErr(e.Message); } } } else { FormCommon.ShowErr("图片文件不存在!"); } }
/// <summary> /// 设置线程,运行copy文件,它与代理CopyFile_Delegate应具有相同的参数和返回类型 /// </summary> private void RunCopyFile() { if (!File.Exists(_DownloadPath)) { Application.Current.Dispatcher.Invoke((Action)(() => { FormCommon.ShowErr("文件:" + _DownloadPath + "不存在!"); this.IsEnabled = true; })); return; } var file = new FileInfo(_DownloadPath); string toFile = this.SavePath + "\\" + file.Name; CopyFile(_DownloadPath, toFile, 1024, btnDownload); //复制文件 Application.Current.Dispatcher.Invoke((Action)(() => { var arge = new MyDownloadEventArge(AfterDownloadRoutedEvent, this); arge._TargetFileInfo = new FileInfo(toFile); RaiseEvent(arge); })); Thread.Sleep(0); //避免假死 ThdCopyFile.Abort(); //关闭线程 }
/// <summary> /// 设置线程,运行copy文件,它与代理CopyFile_Delegate应具有相同的参数和返回类型 /// </summary> private void RunCopyFile() { Application.Current.Dispatcher.Invoke((Action)(() => { if (_FileInfo == null) { _SetErr(); FormCommon.ShowErr("请选择要上传的文件!"); return; } if (!File.Exists(_FileInfo.FullName)) { _SetErr(); FormCommon.ShowErr("源文件不存在!"); return; } })); var path = _DefaultDirectory; try { UploadFile(_FileInfo.FullName, path + _FileInfo.Name, 1024); //复制文件 } catch (Exception ex) { Application.Current.Dispatcher.Invoke((Action)(() => { if (_AfterUpload != null) { _AfterUpload(false, ex.Message); } })); } Thread.Sleep(0); //避免假死 Application.Current.Dispatcher.Invoke((Action)(() => { if (_AfterUpload != null) { _AfterUpload(true, null); } })); ThdCopyFile.Abort(); //关闭线程 }
/// <summary> /// 下载文件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnDownloadFile_Click(object sender, RoutedEventArgs e) { var attachment = sender as AttachmentItem; if (!File.Exists(attachment._FileInfo.FullName)) { FormCommon.ShowErr("文件:" + attachment._FileInfo.FullName + "不存在!"); return; } var op = new System.Windows.Forms.FolderBrowserDialog(); var result = op.ShowDialog(); if (result == System.Windows.Forms.DialogResult.OK) { File.Copy(attachment._FileInfo.FullName, op.SelectedPath + "\\" + attachment._FileInfo.Name, true); } }
private void btnAdd_Click(object sender, MouseButtonEventArgs e) { if (_ViewFullName.IsNullOrEmpty()) { FormCommon.ShowErr("必须将属性“_ViewFullName”设置为当前画面的全称!"); return; } if (this._DefaultDirectory.IsNullOrEmpty()) { FormCommon.ShowErr("必须设置属性“_DefaultDirectory”!"); return; } var op = new System.Windows.Forms.OpenFileDialog(); op.RestoreDirectory = true; var result = op.ShowDialog(); if (result == System.Windows.Forms.DialogResult.OK) { SourceFile = op.FileName; //this._Text = this._Content; this.IsEnabled = true; var arge = new MyUploadEventArge(BeforeAttarchUploadRoutedEvent, this); arge._SourceFileInfo = new FileInfo(op.FileName); RaiseEvent(arge); ThdCopyFile = new Thread(new ThreadStart(RunCopyFile)); // 将子线程管理起来,便宜在画面关闭后将子线程停止 ThreadManager._AddThread(_ViewFullName, ThdCopyFile); // 开始下载 this.IsEnabled = false; ThdCopyFile.Start(); } }
private void btnDownload_Click(object sender, RoutedEventArgs e) { if (_ViewFullName.IsNullOrEmpty()) { FormCommon.ShowErr("必须将属性“_ViewFullName”设置为当前画面的全称!"); return; } var op = new System.Windows.Forms.FolderBrowserDialog(); var result = op.ShowDialog(); if (result == System.Windows.Forms.DialogResult.OK) { this.SavePath = op.SelectedPath; ThdCopyFile = new Thread(new ThreadStart(RunCopyFile)); // 将子线程管理起来,便宜在画面关闭后将子线程停止 ThreadManager._AddThread(_ViewFullName, ThdCopyFile); // 开始下载 this.IsEnabled = false; ThdCopyFile.Start(); } }
/// <summary> /// 复制文件 /// </summary> /// <param name="sourceFile">源文件路径</param> /// <param name="ToFile">目的文件路径</param> /// <param name="TranSize">传输大小</param> /// <param name="progressBar1">ProgressBar控件</param> private void CopyFile(string sourceFile, string ToFile, int TranSize) { if (sourceFile.IsNullOrEmpty()) { FormCommon.ShowErr("未指定源文件!"); return; } // 实例化源文件FileStream类 FileStream FormerOpenStream; // 实例化目标文件FileStream类 FileStream ToFileOpenStream; try { FormerOpenStream = new FileStream(sourceFile, FileMode.Open, FileAccess.Read);//以只读方式打开源文件 } catch (IOException ex) { MessageBox.Show(ex.Message); return; } try { FileStream fileToCreate = new FileStream(ToFile, FileMode.Create); //创建目的文件,如果已存在将被覆盖 fileToCreate.Close(); //关闭所有fileToCreate的资源 fileToCreate.Dispose(); //释放所有fileToCreate的资源 } catch (IOException ex) { MessageBox.Show(ex.Message); return; } ToFileOpenStream = new FileStream(ToFile, FileMode.Append, FileAccess.Write);//以写方式打开目的文件 //根据一次传输的大小,计算最大传输个数. Math.Ceiling 方法 (Double),返回大于或等于指定的双精度浮点数的最小整数值。 int max = Convert.ToInt32(Math.Ceiling((Double)FormerOpenStream.Length / (Double)TranSize)); int currentVal = 0; //progressBar1.Maximum = max;//设置进度条的最大值 int FileSize; //每次要拷贝的文件的大小 if (TranSize < FormerOpenStream.Length) //如果分段拷贝,即每次拷贝内容小于文件总长度 { byte[] buffer = new byte[TranSize]; //根据传输的大小,定义一个字节数组,用来存储传输的字节 int copied = 0; //记录传输的大小 int tem_n = 1; //设置进度栏中进度的增加个数 while (copied <= ((int)FormerOpenStream.Length - TranSize)) { FileSize = FormerOpenStream.Read(buffer, 0, TranSize); //从0开始读到buffer字节数组中,每次最大读TranSize FormerOpenStream.Flush(); //清空缓存 ToFileOpenStream.Write(buffer, 0, TranSize); //向目的文件写入字节 ToFileOpenStream.Flush(); //清空缓存 ToFileOpenStream.Position = FormerOpenStream.Position; //是源文件的目的文件流的位置相同 copied += FileSize; //记录已经拷贝的大小 currentVal += tem_n; //增加进度栏的进度块 Application.Current.Dispatcher.Invoke((Action)(() => { lblPercent.Content = (currentVal * 100 / max).ToString() + "%"; })); } int leftSize = (int)FormerOpenStream.Length - copied; //获取剩余文件的大小 FileSize = FormerOpenStream.Read(buffer, 0, leftSize); //读取剩余的字节 FormerOpenStream.Flush(); ToFileOpenStream.Write(buffer, 0, leftSize); //写入剩余的部分 ToFileOpenStream.Flush(); } else //如果整体拷贝,即每次拷贝内容大于文件总长度 { byte[] buffer = new byte[FormerOpenStream.Length]; FormerOpenStream.Read(buffer, 0, (int)FormerOpenStream.Length); FormerOpenStream.Flush(); ToFileOpenStream.Write(buffer, 0, (int)FormerOpenStream.Length); ToFileOpenStream.Flush(); } FormerOpenStream.Close(); ToFileOpenStream.Close(); Application.Current.Dispatcher.Invoke((Action)(() => { lblPercent.Content = "100%"; var fileInfo = new FileInfo(ToFile); _AttachmentList.Add(fileInfo); MyTimer.SetTimeout(1000, () => { lblPercent.Content = ""; CreateSmallImg(fileInfo, _AttachmentList.Count - 1); }); this.IsEnabled = true; var arge = new MyUploadEventArge(AftervUploadRoutedEvent, this); arge._SourceFileInfo = new FileInfo(sourceFile); arge._TargetFileInfo = new FileInfo(ToFile); RaiseEvent(arge); })); }
/// <summary> /// 复制文件 /// </summary> /// <param name="sourceFile">源文件路径</param> /// <param name="ToFile">目的文件路径</param> /// <param name="TranSize">传输大小</param> /// <param name="progressBar1">ProgressBar控件</param> private void UploadFile(string sourceFile, string ToFile, int TranSize) { if (sourceFile.IsNullOrEmpty()) { FormCommon.ShowErr("未指定源文件!"); return; } // 实例化源文件FileStream类 FileStream FormerOpenStream; // 实例化目标文件FileStream类 FileStream ToFileOpenStream; try { FormerOpenStream = new FileStream(sourceFile, FileMode.Open, FileAccess.Read);//以只读方式打开源文件 } catch (IOException ex) { if (_AfterUpload != null) { _AfterUpload(false, ex.Message); } return; } try { FileStream fileToCreate = new FileStream(ToFile, FileMode.Create); //创建目的文件,如果已存在将被覆盖 fileToCreate.Close(); //关闭所有fileToCreate的资源 fileToCreate.Dispose(); //释放所有fileToCreate的资源 } catch (IOException ex) { if (_AfterUpload != null) { _AfterUpload(false, ex.Message); } return; } ToFileOpenStream = new FileStream(ToFile, FileMode.Append, FileAccess.Write);//以写方式打开目的文件 //根据一次传输的大小,计算最大传输个数. Math.Ceiling 方法 (Double),返回大于或等于指定的双精度浮点数的最小整数值。 int max = Convert.ToInt32(Math.Ceiling((Double)FormerOpenStream.Length / (Double)TranSize)); int currentVal = 0; //progressBar1.Maximum = max;//设置进度条的最大值 int FileSize; //每次要拷贝的文件的大小 if (TranSize < FormerOpenStream.Length) //如果分段拷贝,即每次拷贝内容小于文件总长度 { byte[] buffer = new byte[TranSize]; //根据传输的大小,定义一个字节数组,用来存储传输的字节 int copied = 0; //记录传输的大小 int tem_n = 1; //设置进度栏中进度的增加个数 while (copied <= ((int)FormerOpenStream.Length - TranSize)) { FileSize = FormerOpenStream.Read(buffer, 0, TranSize); //从0开始读到buffer字节数组中,每次最大读TranSize FormerOpenStream.Flush(); //清空缓存 ToFileOpenStream.Write(buffer, 0, TranSize); //向目的文件写入字节 ToFileOpenStream.Flush(); //清空缓存 ToFileOpenStream.Position = FormerOpenStream.Position; //是源文件的目的文件流的位置相同 copied += FileSize; //记录已经拷贝的大小 currentVal += tem_n; //增加进度栏的进度块 Application.Current.Dispatcher.Invoke((Action)(() => { this._Text = _Caption + (currentVal * 100 / max).ToString() + "%"; })); } int leftSize = (int)FormerOpenStream.Length - copied; //获取剩余文件的大小 FileSize = FormerOpenStream.Read(buffer, 0, leftSize); //读取剩余的字节 FormerOpenStream.Flush(); ToFileOpenStream.Write(buffer, 0, leftSize); //写入剩余的部分 ToFileOpenStream.Flush(); Application.Current.Dispatcher.Invoke((Action)(() => { this._Text = _Caption + "100%"; Thread.Sleep(1000); })); } else //如果整体拷贝,即每次拷贝内容大于文件总长度 { byte[] buffer = new byte[FormerOpenStream.Length]; FormerOpenStream.Read(buffer, 0, (int)FormerOpenStream.Length); FormerOpenStream.Flush(); ToFileOpenStream.Write(buffer, 0, (int)FormerOpenStream.Length); ToFileOpenStream.Flush(); } FormerOpenStream.Close(); ToFileOpenStream.Close(); Application.Current.Dispatcher.Invoke((Action)(() => { this._Text = this._Caption; this.IsEnabled = true; })); }
/// <summary> /// 验证条件 /// 遍历该容器下所有继承IInputControl接口的控件的_Value属性,当被遍历的控件的_MustInput属性为true, /// 并且没有输入时会返回这些控件的_Caption,用逗号分隔 /// </summary> /// <returns></returns> public string _CheckCondition() { var listMustInput = new List <IInputControl>(); GetMustInputCondition(this, listMustInput); var list = new List <string>(); var msg = new StringBuilder(); foreach (var contr in listMustInput) { // 对日期范围控件特殊处理 if (contr is MyDatePickerRange) { var datePickerRange = contr as MyDatePickerRange; string from = datePickerRange._Value.ToStr(); string to = datePickerRange._Value2.ToStr(); if (!from.IsNullOrEmpty() && !from.IsNullOrEmpty()) { if (from.CompareTo(to) > 0) { list.Add(contr._Caption); contr._SetErr(); msg.AppendLine(contr._Caption + "的大小关系不正确!"); } } if (from.IsNullOrEmpty() && to.IsNullOrEmpty()) { list.Add(contr._Caption); contr._SetErr(); } else { contr._CleanErr(); } } else { if (contr._Value.ToStr().IsNullOrEmpty()) { list.Add(contr._Caption); contr._SetErr(); } else { contr._CleanErr(); } } } string errControls = string.Join(",", list); if (!errControls.IsNullOrEmpty()) { msg.AppendLine(errControls + "必须输入!"); } if (!msg.ToString().IsNullOrEmpty()) { FormCommon.ShowErr(msg.ToString()); } return(errControls); }