更新时间:2022-08-13 12:35:23
原文 http://support.microsoft.com/kb/839300/zh-cn
using System; using System.Collections; using System.ComponentModel; using System.Drawing; using System.Data; using System.Windows.Forms; using System.IO; using System.Security; using System.Security.Permissions; using System.Reflection; using System.Runtime.CompilerServices; [assembly:AllowPartiallyTrustedCallers] namespace UserControl { /// <summary> /// Summary description for UserControl1. /// </summary> public class UserControl1 : System.Windows.Forms.UserControl { private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.ListBox listBox1; /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.Container components = null; public UserControl1() { // This call is required by the Windows.Forms Form Designer. InitializeComponent(); // TODO: Add any initialization after the InitForm call. OpenFileDialog fileDialog = new OpenFileDialog(); if(fileDialog.ShowDialog() == DialogResult.OK) { // Reading the name of the selected file from the OpenFileDialog box // and reading the file requires FileIOPermission. // The Assert command must be followed by a RevertAssert as soon as the file operation // is completed. new FileIOPermission(PermissionState.Unrestricted).Assert(); textBox1.Text = fileDialog.FileName; // Display the contents of the file in the text box. FileStream fsIn = new FileStream(textBox1.Text, FileMode.Open, FileAccess.Read, FileShare.Read); StreamReader sr = new StreamReader(fsIn); // Process every line in the file. for (String Line = sr.ReadLine(); Line != null; Line = sr.ReadLine()) { listBox1.Items.Add(Line); } // It is very important to call RevertAssert to restore the stack walk for // file operations. FileIOPermission.RevertAssert(); } } /// <summary> /// Clean up any resources that are being used. /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if( components != null ) components.Dispose(); } base.Dispose( disposing ); } #region Component Designer generated code /// <summary> /// Required method for Designer support. Do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.textBox1 = new System.Windows.Forms.TextBox(); this.listBox1 = new System.Windows.Forms.ListBox(); this.SuspendLayout(); // // textBox1 // this.textBox1.Location = new System.Drawing.Point(32, 16); this.textBox1.Name = "textBox1"; this.textBox1.TabIndex = 0; this.textBox1.Text = "textBox1"; // // listBox1 // this.listBox1.Location = new System.Drawing.Point(144, 16); this.listBox1.Name = "listBox1"; this.listBox1.Size = new System.Drawing.Size(120, 95); this.listBox1.TabIndex = 1; // // UserControl1 // this.Controls.Add(this.listBox1); this.Controls.Add(this.textBox1); this.Name = "UserControl1"; this.Size = new System.Drawing.Size(376, 120); this.ResumeLayout(false); } #endregion } }
[assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyKeyFile("")]
[assembly: AssemblyKeyFile("c:\\snKey.snk")]
<OBJECT id="MyWinControl1" height="200" width="200" classid="http:UserControl.dll#UserControl.UserControl1" VIEWASTEXT> </OBJECT>