.net中activex的替代技术:winform control(一)
我想做用
vc
做一
activex
控件对初始编程的人来说可能不是一件容易的事吧,在
.net
出现之后,有更简单的技术来替代它,那就是
winform control
,它可以把做好的
winform control
直接嵌入到
IE
中,对于访问本地磁盘和注册表的
winform control
,可能要重新设置安全策略。我们就拿多文件上传为例,进行讲述。
(一) 做一winform control
上面的代码中,公布了Uri和ServerFolder属性:Uri指定上传的url地址,ServerFolder指定存放的上传文件的文件夹;这俩个属性都是可读可写的,可以在网页中通过javascript和用户交互。到此为止,上传的winform控件就完成了,非常简单。(未完待续)
using
System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Net;
using System.IO;
namespace MultiFileUpload
{
/**//// <summary>
/// UserControl1 的摘要说明。
/// </summary>
public class Demo : System.Windows.Forms.UserControl
{
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Splitter splitter1;
private System.Windows.Forms.Panel panel2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Splitter splitter2;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
public WebClient wc=new WebClient();
private string filestr="";
private string[] FileListArr;
private System.Windows.Forms.Splitter splitter3;
private System.Windows.Forms.ProgressBar progressBar1;
private System.Windows.Forms.Splitter splitter4;
private System.Windows.Forms.Panel panel3;
private System.Windows.Forms.ListBox listBox1;
private string uri="http://10.126.65.96/WebApplication2";//根据自己的情况设置
private string serverfolder="usercontrols";//根据自己的情况设置
/**//// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Demo()
{
// 该调用是 Windows.Forms 窗体设计器所必需的。
InitializeComponent();
// TODO: 在 InitComponent 调用后添加任何初始化
}
/**//// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if( components != null )
components.Dispose();
}
base.Dispose( disposing );
}
public string Uri
{
set
{
this.uri=value;
}
get
{
return this.uri;
}
}
public string ServerFolder
{
set
{
this.serverfolder=value;
}
get
{
return this.serverfolder;
}
}
组件设计器生成的代码#region 组件设计器生成的代码
/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Demo));
this.panel1 = new System.Windows.Forms.Panel();
this.splitter3 = new System.Windows.Forms.Splitter();
this.panel2 = new System.Windows.Forms.Panel();
this.button2 = new System.Windows.Forms.Button();
this.splitter2 = new System.Windows.Forms.Splitter();
this.button1 = new System.Windows.Forms.Button();
this.splitter1 = new System.Windows.Forms.Splitter();
this.label1 = new System.Windows.Forms.Label();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.progressBar1 = new System.Windows.Forms.ProgressBar();
this.splitter4 = new System.Windows.Forms.Splitter();
this.panel3 = new System.Windows.Forms.Panel();
this.listBox1 = new System.Windows.Forms.ListBox();
this.panel1.SuspendLayout();
this.panel2.SuspendLayout();
this.panel3.SuspendLayout();
this.SuspendLayout();
//
// panel1
//
this.panel1.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("panel1.BackgroundImage")));
this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.panel1.Controls.Add(this.panel3);
this.panel1.Controls.Add(this.splitter4);
this.panel1.Controls.Add(this.progressBar1);
this.panel1.Controls.Add(this.splitter3);
this.panel1.Controls.Add(this.panel2);
this.panel1.Controls.Add(this.splitter1);
this.panel1.Controls.Add(this.label1);
this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
this.panel1.Location = new System.Drawing.Point(0, 0);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(568, 376);
this.panel1.TabIndex = 0;
//
// splitter3
//
this.splitter3.Dock = System.Windows.Forms.DockStyle.Top;
this.splitter3.Location = new System.Drawing.Point(72, 43);
this.splitter3.Name = "splitter3";
this.splitter3.Size = new System.Drawing.Size(494, 3);
this.splitter3.TabIndex = 4;
this.splitter3.TabStop = false;
//
// panel2
//
this.panel2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.panel2.Controls.Add(this.button2);
this.panel2.Controls.Add(this.splitter2);
this.panel2.Controls.Add(this.button1);
this.panel2.Dock = System.Windows.Forms.DockStyle.Left;
this.panel2.Location = new System.Drawing.Point(0, 43);
this.panel2.Name = "panel2";
this.panel2.Size = new System.Drawing.Size(72, 331);
this.panel2.TabIndex = 2;
//
// button2
//
this.button2.Dock = System.Windows.Forms.DockStyle.Fill;
this.button2.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button2.Location = new System.Drawing.Point(0, 155);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(70, 174);
this.button2.TabIndex = 2;
this.button2.Text = "上传文件";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// splitter2
//
this.splitter2.Dock = System.Windows.Forms.DockStyle.Top;
this.splitter2.Location = new System.Drawing.Point(0, 152);
this.splitter2.Name = "splitter2";
this.splitter2.Size = new System.Drawing.Size(70, 3);
this.splitter2.TabIndex = 1;
this.splitter2.TabStop = false;
//
// button1
//
this.button1.Dock = System.Windows.Forms.DockStyle.Top;
this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button1.Location = new System.Drawing.Point(0, 0);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(70, 152);
this.button1.TabIndex = 0;
this.button1.Text = "选择文件";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// splitter1
//
this.splitter1.Dock = System.Windows.Forms.DockStyle.Top;
this.splitter1.Location = new System.Drawing.Point(0, 40);
this.splitter1.Name = "splitter1";
this.splitter1.Size = new System.Drawing.Size(566, 3);
this.splitter1.TabIndex = 1;
this.splitter1.TabStop = false;
//
// label1
//
this.label1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.label1.Dock = System.Windows.Forms.DockStyle.Top;
this.label1.Location = new System.Drawing.Point(0, 0);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(566, 40);
this.label1.TabIndex = 0;
this.label1.Text = "多文件上传";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// progressBar1
//
this.progressBar1.Dock = System.Windows.Forms.DockStyle.Bottom;
this.progressBar1.Location = new System.Drawing.Point(72, 351);
this.progressBar1.Name = "progressBar1";
this.progressBar1.Size = new System.Drawing.Size(494, 23);
this.progressBar1.TabIndex = 5;
//
// splitter4
//
this.splitter4.Dock = System.Windows.Forms.DockStyle.Bottom;
this.splitter4.Location = new System.Drawing.Point(72, 349);
this.splitter4.Name = "splitter4";
this.splitter4.Size = new System.Drawing.Size(494, 2);
this.splitter4.TabIndex = 6;
this.splitter4.TabStop = false;
//
// panel3
//
this.panel3.Controls.Add(this.listBox1);
this.panel3.Dock = System.Windows.Forms.DockStyle.Fill;
this.panel3.Location = new System.Drawing.Point(72, 46);
this.panel3.Name = "panel3";
this.panel3.Size = new System.Drawing.Size(494, 303);
this.panel3.TabIndex = 7;
//
// listBox1
//
this.listBox1.Dock = System.Windows.Forms.DockStyle.Fill;
this.listBox1.ItemHeight = 12;
this.listBox1.Location = new System.Drawing.Point(0, 0);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(494, 292);
this.listBox1.TabIndex = 0;
//
// Demo
//
this.Controls.Add(this.panel1);
this.Name = "Demo";
this.Size = new System.Drawing.Size(568, 376);
this.panel1.ResumeLayout(false);
this.panel2.ResumeLayout(false);
this.panel3.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
public string GetUri()
{
return this.uri;
}
public void SendFiles()
{
char[] ch=new char[1];
ch[0]='/';
string[] arrstr=this.filestr.Split(ch);
WebClient myWebClient = new WebClient();
做一存取凭据#region 做一存取凭据
//NetworkCredential myCred = new NetworkCredential("Administrator","11");//
//CredentialCache myCache = new CredentialCache();
//myCache.Add(new Uri(this.uri), "Basic", myCred);
//myWebClient.Credentials =myCache;
#endregion
//使用默认的权限
myWebClient.Credentials =CredentialCache.DefaultCredentials;
int count=this.listBox1.Items.Count;
this.progressBar1.Maximum=10*count;
for(int i=0;i<arrstr.Length;i++)
{
try
{
string fileNamePath=arrstr[i];
string fileName = fileNamePath.Substring(fileNamePath.LastIndexOf("//") + 1);
string uriString = this.uri+"/"+this.serverfolder+"/" + fileName;//指定上传得路径
// 要上传的文件
FileStream fs = new FileStream(fileNamePath, FileMode.Open, FileAccess.Read);
//FileStream fs = OpenFile();
BinaryReader r = new BinaryReader(fs);
//使用UploadFile方法可以用下面的格式
// myWebClient.UploadFile(uriString,"PUT",fileNamePath);
// MessageBox.Show(uriString);
byte[] postArray = r.ReadBytes((int)fs.Length);
Stream postStream = myWebClient.OpenWrite(uriString,"PUT");
if(postStream.CanWrite)
{
postStream.Write(postArray,0,postArray.Length);
this.progressBar1.Value=(i+1)*10;
}
else
{
MessageBox.Show( "文件目前不可写!");
}
postStream.Close();
}
catch(WebException errMsg)
{
MessageBox.Show("上传失败:" + errMsg.Message);
break;
}
}
if(this.progressBar1.Value==this.progressBar1.Maximum)
{
MessageBox.Show("成功");
}
else
{
MessageBox.Show("传送中出现错误!");
}
}
private void button1_Click(object sender, System.EventArgs e)
{
OpenFileDialog openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
openFileDialog1.Multiselect = true;
openFileDialog1.RestoreDirectory = true;
openFileDialog1.Filter="dwg文件|*.dwg";
openFileDialog1.ShowDialog();
this.FileListArr=openFileDialog1.FileNames;
this.listBox1.Items.Clear();
for(int i=0;i<this.FileListArr.Length;i++)
{
this.listBox1.Items.Add(this.FileListArr[i]);
}
this.filestr="";
for(int i=0;i<this.FileListArr.Length;i++)
{
if(this.filestr=="")
{
this.filestr=this.FileListArr[i];
}
else
{
this.filestr=this.filestr+"/"+this.FileListArr[i];
}
}
}
private void button2_Click(object sender, System.EventArgs e)
{
this.SendFiles();
}
}
}
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Net;
using System.IO;
namespace MultiFileUpload
{
/**//// <summary>
/// UserControl1 的摘要说明。
/// </summary>
public class Demo : System.Windows.Forms.UserControl
{
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Splitter splitter1;
private System.Windows.Forms.Panel panel2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Splitter splitter2;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
public WebClient wc=new WebClient();
private string filestr="";
private string[] FileListArr;
private System.Windows.Forms.Splitter splitter3;
private System.Windows.Forms.ProgressBar progressBar1;
private System.Windows.Forms.Splitter splitter4;
private System.Windows.Forms.Panel panel3;
private System.Windows.Forms.ListBox listBox1;
private string uri="http://10.126.65.96/WebApplication2";//根据自己的情况设置
private string serverfolder="usercontrols";//根据自己的情况设置
/**//// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Demo()
{
// 该调用是 Windows.Forms 窗体设计器所必需的。
InitializeComponent();
// TODO: 在 InitComponent 调用后添加任何初始化
}
/**//// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if( components != null )
components.Dispose();
}
base.Dispose( disposing );
}
public string Uri
{
set
{
this.uri=value;
}
get
{
return this.uri;
}
}
public string ServerFolder
{
set
{
this.serverfolder=value;
}
get
{
return this.serverfolder;
}
}
组件设计器生成的代码#region 组件设计器生成的代码
/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Demo));
this.panel1 = new System.Windows.Forms.Panel();
this.splitter3 = new System.Windows.Forms.Splitter();
this.panel2 = new System.Windows.Forms.Panel();
this.button2 = new System.Windows.Forms.Button();
this.splitter2 = new System.Windows.Forms.Splitter();
this.button1 = new System.Windows.Forms.Button();
this.splitter1 = new System.Windows.Forms.Splitter();
this.label1 = new System.Windows.Forms.Label();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.progressBar1 = new System.Windows.Forms.ProgressBar();
this.splitter4 = new System.Windows.Forms.Splitter();
this.panel3 = new System.Windows.Forms.Panel();
this.listBox1 = new System.Windows.Forms.ListBox();
this.panel1.SuspendLayout();
this.panel2.SuspendLayout();
this.panel3.SuspendLayout();
this.SuspendLayout();
//
// panel1
//
this.panel1.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("panel1.BackgroundImage")));
this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.panel1.Controls.Add(this.panel3);
this.panel1.Controls.Add(this.splitter4);
this.panel1.Controls.Add(this.progressBar1);
this.panel1.Controls.Add(this.splitter3);
this.panel1.Controls.Add(this.panel2);
this.panel1.Controls.Add(this.splitter1);
this.panel1.Controls.Add(this.label1);
this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
this.panel1.Location = new System.Drawing.Point(0, 0);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(568, 376);
this.panel1.TabIndex = 0;
//
// splitter3
//
this.splitter3.Dock = System.Windows.Forms.DockStyle.Top;
this.splitter3.Location = new System.Drawing.Point(72, 43);
this.splitter3.Name = "splitter3";
this.splitter3.Size = new System.Drawing.Size(494, 3);
this.splitter3.TabIndex = 4;
this.splitter3.TabStop = false;
//
// panel2
//
this.panel2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.panel2.Controls.Add(this.button2);
this.panel2.Controls.Add(this.splitter2);
this.panel2.Controls.Add(this.button1);
this.panel2.Dock = System.Windows.Forms.DockStyle.Left;
this.panel2.Location = new System.Drawing.Point(0, 43);
this.panel2.Name = "panel2";
this.panel2.Size = new System.Drawing.Size(72, 331);
this.panel2.TabIndex = 2;
//
// button2
//
this.button2.Dock = System.Windows.Forms.DockStyle.Fill;
this.button2.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button2.Location = new System.Drawing.Point(0, 155);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(70, 174);
this.button2.TabIndex = 2;
this.button2.Text = "上传文件";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// splitter2
//
this.splitter2.Dock = System.Windows.Forms.DockStyle.Top;
this.splitter2.Location = new System.Drawing.Point(0, 152);
this.splitter2.Name = "splitter2";
this.splitter2.Size = new System.Drawing.Size(70, 3);
this.splitter2.TabIndex = 1;
this.splitter2.TabStop = false;
//
// button1
//
this.button1.Dock = System.Windows.Forms.DockStyle.Top;
this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button1.Location = new System.Drawing.Point(0, 0);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(70, 152);
this.button1.TabIndex = 0;
this.button1.Text = "选择文件";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// splitter1
//
this.splitter1.Dock = System.Windows.Forms.DockStyle.Top;
this.splitter1.Location = new System.Drawing.Point(0, 40);
this.splitter1.Name = "splitter1";
this.splitter1.Size = new System.Drawing.Size(566, 3);
this.splitter1.TabIndex = 1;
this.splitter1.TabStop = false;
//
// label1
//
this.label1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.label1.Dock = System.Windows.Forms.DockStyle.Top;
this.label1.Location = new System.Drawing.Point(0, 0);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(566, 40);
this.label1.TabIndex = 0;
this.label1.Text = "多文件上传";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// progressBar1
//
this.progressBar1.Dock = System.Windows.Forms.DockStyle.Bottom;
this.progressBar1.Location = new System.Drawing.Point(72, 351);
this.progressBar1.Name = "progressBar1";
this.progressBar1.Size = new System.Drawing.Size(494, 23);
this.progressBar1.TabIndex = 5;
//
// splitter4
//
this.splitter4.Dock = System.Windows.Forms.DockStyle.Bottom;
this.splitter4.Location = new System.Drawing.Point(72, 349);
this.splitter4.Name = "splitter4";
this.splitter4.Size = new System.Drawing.Size(494, 2);
this.splitter4.TabIndex = 6;
this.splitter4.TabStop = false;
//
// panel3
//
this.panel3.Controls.Add(this.listBox1);
this.panel3.Dock = System.Windows.Forms.DockStyle.Fill;
this.panel3.Location = new System.Drawing.Point(72, 46);
this.panel3.Name = "panel3";
this.panel3.Size = new System.Drawing.Size(494, 303);
this.panel3.TabIndex = 7;
//
// listBox1
//
this.listBox1.Dock = System.Windows.Forms.DockStyle.Fill;
this.listBox1.ItemHeight = 12;
this.listBox1.Location = new System.Drawing.Point(0, 0);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(494, 292);
this.listBox1.TabIndex = 0;
//
// Demo
//
this.Controls.Add(this.panel1);
this.Name = "Demo";
this.Size = new System.Drawing.Size(568, 376);
this.panel1.ResumeLayout(false);
this.panel2.ResumeLayout(false);
this.panel3.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
public string GetUri()
{
return this.uri;
}
public void SendFiles()
{
char[] ch=new char[1];
ch[0]='/';
string[] arrstr=this.filestr.Split(ch);
WebClient myWebClient = new WebClient();
做一存取凭据#region 做一存取凭据
//NetworkCredential myCred = new NetworkCredential("Administrator","11");//
//CredentialCache myCache = new CredentialCache();
//myCache.Add(new Uri(this.uri), "Basic", myCred);
//myWebClient.Credentials =myCache;
#endregion
//使用默认的权限
myWebClient.Credentials =CredentialCache.DefaultCredentials;
int count=this.listBox1.Items.Count;
this.progressBar1.Maximum=10*count;
for(int i=0;i<arrstr.Length;i++)
{
try
{
string fileNamePath=arrstr[i];
string fileName = fileNamePath.Substring(fileNamePath.LastIndexOf("//") + 1);
string uriString = this.uri+"/"+this.serverfolder+"/" + fileName;//指定上传得路径
// 要上传的文件
FileStream fs = new FileStream(fileNamePath, FileMode.Open, FileAccess.Read);
//FileStream fs = OpenFile();
BinaryReader r = new BinaryReader(fs);
//使用UploadFile方法可以用下面的格式
// myWebClient.UploadFile(uriString,"PUT",fileNamePath);
// MessageBox.Show(uriString);
byte[] postArray = r.ReadBytes((int)fs.Length);
Stream postStream = myWebClient.OpenWrite(uriString,"PUT");
if(postStream.CanWrite)
{
postStream.Write(postArray,0,postArray.Length);
this.progressBar1.Value=(i+1)*10;
}
else
{
MessageBox.Show( "文件目前不可写!");
}
postStream.Close();
}
catch(WebException errMsg)
{
MessageBox.Show("上传失败:" + errMsg.Message);
break;
}
}
if(this.progressBar1.Value==this.progressBar1.Maximum)
{
MessageBox.Show("成功");
}
else
{
MessageBox.Show("传送中出现错误!");
}
}
private void button1_Click(object sender, System.EventArgs e)
{
OpenFileDialog openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
openFileDialog1.Multiselect = true;
openFileDialog1.RestoreDirectory = true;
openFileDialog1.Filter="dwg文件|*.dwg";
openFileDialog1.ShowDialog();
this.FileListArr=openFileDialog1.FileNames;
this.listBox1.Items.Clear();
for(int i=0;i<this.FileListArr.Length;i++)
{
this.listBox1.Items.Add(this.FileListArr[i]);
}
this.filestr="";
for(int i=0;i<this.FileListArr.Length;i++)
{
if(this.filestr=="")
{
this.filestr=this.FileListArr[i];
}
else
{
this.filestr=this.filestr+"/"+this.FileListArr[i];
}
}
}
private void button2_Click(object sender, System.EventArgs e)
{
this.SendFiles();
}
}
}