/// <summary> /// 准备数据源用于表格显示,主要功能:自动生成显示图标。 /// </summary> public void DoPrepareDataSource() { if (_attachmentStorage == null) { return; } //临时显示字段 if (_attachmentStorage.Columns.IndexOf(tb_AttachFile.IconLarge) < 0) { _attachmentStorage.Columns.Add(tb_AttachFile.IconLarge, typeof(byte[])); } if (_attachmentStorage.Columns.IndexOf(tb_AttachFile.IconSmall) < 0) { _attachmentStorage.Columns.Add(tb_AttachFile.IconSmall, typeof(byte[])); } Icon large; Icon small; foreach (DataRow R in _attachmentStorage.Rows) { if (R.RowState == DataRowState.Deleted) { continue; } if (R[tb_AttachFile.IconSmall] != DBNull.Value) { continue; } AttachTool.CreateFileIcon(R, out large, out small, true); //创建图标,用于表格显示 } }
/// <summary> /// 获取文件的ICO图标 /// </summary> /// <param name="fileType">文件类型(*.exe,*.dll)</param> /// <param name="large">大图标</param> /// <param name="small">返回小图标</param> public static void CreateFileIcon(string fileType, out Icon large, out Icon small) { string des; if (fileType.Trim() == "") //预设图标 { AttachTool.GetDefaultIcon(out large, out small); } else if (fileType.ToUpper() == ".EXE") //应用程序图标单独获取 { IntPtr l = IntPtr.Zero; IntPtr s = IntPtr.Zero; AttachTool.ExtractIconExW(Path.Combine(Environment.SystemDirectory, "shell32.dll"), 2, ref l, ref s, 1); large = Icon.FromHandle(l); small = Icon.FromHandle(s); } else //其它类型的图标 { AttachTool.GetExtsIconAndDescription(fileType, out large, out small, out des); } if ((large == null) || (small == null)) //无法获取图标,预设图标 { AttachTool.GetDefaultIcon(out large, out small); } }
public void AddFile(string fileFullName) { FileStream fs = null; try { string ext = Path.GetExtension(fileFullName); //扩展名 string fileName = Path.GetFileName(fileFullName); //显示文件名称 fs = new FileStream(fileFullName, FileMode.Open); byte[] bs = new byte[fs.Length]; //文件内容 fs.Read(bs, 0, (int)fs.Length); //读取文件内容 decimal size = decimal.Round((decimal)(fs.Length / 1024.00), 2); //文件大小 kb fs.Close(); //显示文件的图标 Icon large; Icon small; AttachTool.CreateFileIcon(ext, out large, out small); //获取文件的图标 TAttachFile file = new TAttachFile(); //附件对象 file.FileName = fileName; file.FileTitle = fileName; //文件标题 file.FileType = ext; ////扩展名 file.FileSize = size; file.FileBody = bs; file.IconLarge = AttachTool.ImageToByte(large.ToBitmap()); //大图标 file.IconSmall = AttachTool.ImageToByte(small.ToBitmap()); //小图标 this.AddFile(file); //保存数据 } catch { if (fs != null) { fs.Close(); } } }