您的位置:首页 > 编程语言 > C#

c# winform项目treeview控件绑定本地电脑磁盘信息,浏览磁盘图片,上传本地图片控件

2010-08-10 09:16 716 查看
这篇文章是我在之前那篇文章上的深入,可以获取到列表后,在右边将图片显示出来,并添加了上传功能,好啦,不多说了,代码如下

首先是winform的布局代码

namespace TreeviewTest
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows 窗体设计器生成的代码

/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.treeView1 = new System.Windows.Forms.TreeView();
this.panel1 = new System.Windows.Forms.Panel();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// treeView1
//
this.treeView1.Location = new System.Drawing.Point(13, 13);
this.treeView1.Name = "treeView1";
this.treeView1.Size = new System.Drawing.Size(217, 415);
this.treeView1.TabIndex = 0;
//
// panel1
//
this.panel1.AutoScroll = true;
this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.panel1.Location = new System.Drawing.Point(237, 42);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(559, 386);
this.panel1.TabIndex = 1;
//
// button1
//
this.button1.Location = new System.Drawing.Point(237, 13);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 2;
this.button1.Text = "全选";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(331, 13);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 3;
this.button2.Text = "反选";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(721, 12);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(75, 23);
this.button3.TabIndex = 4;
this.button3.Text = "上传";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(808, 440);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.panel1);
this.Controls.Add(this.treeView1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}

#endregion

private System.Windows.Forms.TreeView treeView1;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
}
}

接下是后台功能实现的代码

namespace TreeviewTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
CpuMessage();
this.treeView1.NodeMouseClick += new TreeNodeMouseClickEventHandler(treeView1_NodeMouseClick);
}

public void CpuMessage()
{
ImageList imglist = new ImageList();
imglist.Images.Add(Image.FromFile(Application.StartupPath + "//iamges//1.jpg"));
imglist.Images.Add(Image.FromFile(Application.StartupPath + "//iamges//2.jpg"));
imglist.Images.Add(Image.FromFile(Application.StartupPath + "//iamges//3.jpg"));
this.treeView1.ImageList = imglist;
DriveInfo[] dr = DriveInfo.GetDrives();
string driveName = "";
foreach (DriveInfo d in dr)
{
TreeNode tn = new TreeNode();
tn.Name = d.Name;
switch (d.DriveType)
{
case DriveType.Fixed:
tn.ImageIndex = 0;
driveName = "本地磁盘(" + d.Name.Substring(0,2) + ")";
break;
case DriveType.Removable:
tn.ImageIndex = 0;
driveName = "可移动磁盘(" + d.Name.Substring(0, 2) + ")";
break;
case DriveType.CDRom:
tn.ImageIndex = 1;
driveName = "DVD驱动器(" + d.Name.Substring(0, 2) + ")";
break;
case DriveType.Network:
driveName = "网络驱动器(" + d.Name.Substring(0, 2) + ")";
break;
default :
driveName = "未知(" + d.Name + ")";
break;
}
tn.Text = driveName;
this.treeView1.Nodes.Add(tn);
}
}

void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
if (e.Node.Nodes.Count > 0)
{
if (e.Node.IsExpanded)
{
e.Node.Collapse();

}
else
{
e.Node.Expand();
}
}
else
{
if (Directory.Exists(e.Node.Name))
{
panel1.Controls.Clear();
try
{
string[] allDirectory = Directory.GetDirectories(e.Node.Name);
foreach (string s in allDirectory)
{
TreeNode tn = new TreeNode();
tn.Name = s;
tn.Text = s.Remove(0, s.LastIndexOf("//") + 1);
tn.ImageIndex = 2;
e.Node.Nodes.Add(tn);
}
List<string> allFiles = Directory.GetFiles(e.Node.Name,"*.jpg").ToList<string>();
allFiles.AddRange(Directory.GetFiles(e.Node.Name, "*.gif").ToList<string>());
int numindex = 1;
foreach (string sf in allFiles)
{
ImgButton ib = new ImgButton();
PictureBox pb = new PictureBox();
Image img = Bitmap.FromFile(sf);
pb.Image = img;
pb.SizeMode = PictureBoxSizeMode.CenterImage;
pb.BorderStyle = BorderStyle.FixedSingle;
ib.pictureBox.Image = img;
ib.pictureBox.SizeMode = PictureBoxSizeMode.CenterImage;
ib.Name = "picselect";
if (numindex % 4 == 0)
{
ib.Location = new Point(ib.Width * (numindex % 4 + 3) + 20 * 4, ib.Height * (numindex / 4 - 1) + 20 * (numindex / 4 - 1));
}
else
{
ib.Location = new Point(ib.Width * (numindex % 4 - 1) + 20 * (numindex % 4), ib.Height * (numindex / 4) + 20 * (numindex / 4));
}
ib.label.Text = sf.Remove(0, sf.LastIndexOf("//") + 1);
ib.path.Text = sf;
panel1.Controls.Add(ib);
numindex++;
}
}
catch
{
}
}
e.Node.Expand();
}
}

private void button3_Click(object sender, EventArgs e)
{
List<Control> conlist = this.panel1.Controls.Find("picselect", true).ToList<Control>();
foreach (Control ctr in conlist)
{
ImgButton imgButton = ctr as ImgButton;
if (imgButton.checkBox.Checked)
{
FileUpload(imgButton.path.Text, @"F:/Test/myTestWeb/uploads/", imgButton.label.Text);
}
}

}

void FileUpload(string localPathFile, string webPhysicalPathFile, string filename)
{
WebClient myWebClient = new WebClient();
myWebClient.Credentials = CredentialCache.DefaultCredentials;

FileStream fs = new FileStream(localPathFile, FileMode.Open, FileAccess.Read);
BinaryReader r = new BinaryReader(fs);
try
{
byte[] postArray = r.ReadBytes((int)fs.Length);
Stream postStream = myWebClient.OpenWrite(webPhysicalPathFile + filename, "PUT");
if (postStream.CanWrite)
{
postStream.Write(postArray, 0, postArray.Length);
}
postStream.Close();
}
catch
{
MessageBox.Show("文件上传失败");
}
}

private void button1_Click(object sender, EventArgs e)
{
List<Control> conlist = this.panel1.Controls.Find("picselect", true).ToList<Control>();
foreach (Control ctr in conlist)
{
ImgButton imgButton = ctr as ImgButton;
imgButton.checkBox.Checked = true;
}
}

private void button2_Click(object sender, EventArgs e)
{
List<Control> conlist = this.panel1.Controls.Find("picselect", true).ToList<Control>();
foreach (Control ctr in conlist)
{
ImgButton imgButton = ctr as ImgButton;
imgButton.checkBox.Checked = false;
}
}

}

}

这样就是实现了本地图片批量上传的功能,大家再结合那篇c# active控件开发的技术,就能完成图片上传控件了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: