设为首页
加入收藏
联系我们
首页 基础教程 技术文档 实例分析 数 据 库 疑难杂症 ASP.NET 七夕许愿树 技术论坛
<%-- 最新动态 Begin --%> <%-- 最新动态 End --%>
最新文章
 C#摄像头编程实例
 C#下用P2P技术实现点…
 Asp.net(c#)数…
 SQL Server数据…
 .NET牛人应该知道些什…
 NET牛人应该知道些什么
 SQL Server S…
 备份指定表到另一数据库
 SQLSERVER中快速…
 ASP.NET中url传…
 ASP.NET自动给UR…
 ASP.NET 2.0防…
 ASP.NET 2.0 …
 如何解决ASP.net中…
 sql server 与…
<%-- 最新动态 Begin --%> <%-- 最新动态 End --%>
推荐文章
 关于C#中的REF和黓认…
 读书笔记c#高级编程 委…
 【算法】C#快速排序类
 Visual C#的SQ…
 C#中调用API
 Infragistics…
 C#接口转换
 C#读取设备信息
 用.net操作word
 C# MessageBo…
 Visual C#中的数…
 雅虎公司C#笔试题,看看…
 C#.NET使用NHib…
 .net学习之运算符重载…
 Visual C# 3.…
<%-- 最新动态 Begin --%> <%-- 最新动态 End --%>
热门文章
 ADO.Net与ADO在…
 开发ASP.NET下的M…
 用C#+XMI技术进行U…
 什么是虚拟机?
 C#基础全接触
 C#学习第一天
 雅虎公司C#笔试题,看看…
 C#语言初级入门(1)
 C#中利用正则表达式实现…
 远程重启计算机(C#)
 用.net操作word
 什么是B/S三层?
 VB和C# 语法对比图 …
 Visual C#常用函…
 Visual C#的SQ…
用.net开发activex(一)
阅读正文 文字大小:增大 减小  文字行距:增大 减小   双击自动滚屏
本文由中国C#技术学习中心整理  如果你对本文有不明之处请到技术论坛讨论!

       我想用vc做一activex控件对初始编程的人来说可能不是一件容易的事吧,在.net出现之后,有更简单的技术来替代它,那就是winform control,它可以把做好的winform control直接嵌入到IE中,对于访问本地磁盘和注册表的winform control,可能要重新设置安全策略。我们就拿多文件上传为例,进行讲述。

 

(一)   做一winform control


 

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
{
    
/// 
    
/// UserControl1 的摘要说明。
    
/// 

    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";//根据自己的情况设置
        /// 
        
/// 必需的设计器变量。
        
/// 

        private System.ComponentModel.Container components = null;

        
public Demo()
        
{
            
// 该调用是 Windows.Forms 窗体设计器所必需的。
            InitializeComponent();

            
// TODO: 在 InitComponent 调用后添加任何初始化

        }


        
/// 
        
/// 清理所有正在使用的资源。
        
/// 

        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;
            }

        }

        
组件设计器生成的代码
        
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(); 
            
做一存取凭据
            
//使用默认的权限              
            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();
        }

    }

}

上面的代码中,公布了Uri和ServerFolder属性:Uri指定上传的url地址,ServerFolder指定存放的上传文件的文件夹;这俩个属性都是可读可写的,可以在网页中通过javascript和用户交互。到此为止,上传的winform控件就完成了,非常简单。(未完待续)

本文由中国C#技术学习中心整理  如果你对本文有不明之处请到技术论坛讨论!

中国C#技术交流QQ群:6337034  10976424  9383681  35248582  35248645
版权所有:中国C#技术学习中心 Copyright ? 2006-2008
建议浏览分辨率使用:1024*768分辨率
粤ICP备05002251号