模拟win7运行窗体C#源码

来源:互联网 发布:淘宝客佣金设置技巧 编辑:程序博客网 时间:2024/06/06 00:45

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;


namespace 运行
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 存放运行过的文件
        /// </summary>
        List<string> lstPath = new List<string>();
        //浏览
        private void btnBrowse_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog()==DialogResult.OK)
            {
                this.cboPath.Text = openFileDialog1.FileName;
            }
        }
        //取消
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        //确定
        private void btnAscertain_Click(object sender, EventArgs e)
        {
            try
            {
                Process.Start(cboPath.Text);
                lstPath.Add(cboPath.Text);
                this.Close();
            }
            catch (Exception)
            {             
                MessageBox.Show("Windows找不到文件'"+cboPath.Text+"'。请确定文件名是否正确后,在试一次。",cboPath.Text,MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
        }

        private void cboPath_TextChanged(object sender, EventArgs e)
        {
            if (cboPath.Text.Trim()=="")
            {
                btnAscertain.Enabled = false;
            }
            else
            {
                btnAscertain.Enabled = true;
            }
        }
        //退出时序列化文件
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            FileStream fs = new FileStream(@"C:/Windows/yunxing.bin", FileMode.Create);//改成你喜欢的路径
            BinaryFormatter bf = new BinaryFormatter();
            bf.Serialize(fs, lstPath);
            fs.Close();
        }
        //加载时读取
        private void Form1_Load(object sender, EventArgs e)
        {
            if (File.Exists(@"C:/Windows/yunxing.bin"))
            {
                try
                {
                    FileStream fs = new FileStream("yunxing.bin", FileMode.Open);
                    BinaryFormatter bf = new BinaryFormatter();
                    lstPath = (List<string>)bf.Deserialize(fs);
                    fs.Close();
                    for (int i = lstPath.Count - 1; i >= 0; i--)
                    {
                        cboPath.Items.Add(lstPath[i]);
                    }
                    cboPath.Text = lstPath[lstPath.Count - 1];
                }
                catch (Exception)
                {                   
                    //不处理异常
                }
            }
            this.Location = new Point(0,Screen.GetWorkingArea(this).Height-this.Height);
        }       
    }
}

 

【程序效果图】

原创粉丝点击