欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

运行其他程序-源码分享 源程序已分享

程序员文章站 2022-06-20 19:02:22
...

frm_main.cs
========华丽分界线=======
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;

namespace 运行其他程序
{
public partial class frm_main : Form
{
public frm_main()
{
InitializeComponent();
}
BaseCommon.DirectoryCommon DC_a_E_DirAction = new BaseCommon.DirectoryCommon();
BaseCommon.FileCommon FC_a_E_FileAction = new BaseCommon.FileCommon();
ImageList il_a_e_ProcessIcon = new ImageList();

    private void frm_main_Load(object sender, EventArgs e)
    {
        lv_ShowProcess.View = View.LargeIcon;
        lv_ShowProcess.LargeImageList = il_a_e_ProcessIcon;

        if (!DC_a_E_DirAction.F_E_DirectoryExists("\\config"))
        {
            DC_a_E_DirAction.F_E_DirectoryCreat("\\config");
        }
        if (!FC_a_E_FileAction.F_E_FileExists("\\config\\set.inf"))
        {
            FC_a_E_FileAction.F_E_FileCreate("\\config\\set.inf");
        }

        F_GetDataInfo();
    }
    public void F_GetDataInfo()
    {
        string str_a_e_SetInfo = FC_a_E_FileAction.F_E_ReadDataByString("\\config\\set.inf");
        if (str_a_e_SetInfo != "")
        {
            F_AddProcess(str_a_e_SetInfo.Replace("\r\n", "\n").Split('\n'));
        }
    }
    public void F_AddProcess(string[] strs_t_e_paths)
    {
        for (int i = 0, j = strs_t_e_paths.Length / 2; i < j; i++)
        {
            if (strs_t_e_paths[2 * i] != "")
            {

                if (strs_t_e_paths[2 * i].IndexOf("path=") >= 0 && strs_t_e_paths[2 * i + 1].IndexOf("name=") >= 0)
                {
                    Icon icon = System.Drawing.Icon.ExtractAssociatedIcon(strs_t_e_paths[2 * i].Replace("path=", ""));
                    il_a_e_ProcessIcon.Images.Add(icon);
                }
            }
        }
        this.lv_ShowProcess.BeginUpdate();
        for (int i = 0, j = strs_t_e_paths.Length / 2; i < j; i++)
        {
            if (strs_t_e_paths[2 * i] != "")
            {
                if (strs_t_e_paths[2 * i].IndexOf("path=") >= 0 && strs_t_e_paths[2 * i + 1].IndexOf("name=") >= 0)
                {
                    F_AddProcess(lv_ShowProcess, strs_t_e_paths[2 * i].Replace("path=", ""), strs_t_e_paths[2 * i + 1].Replace("name=", ""), i);
                }
            }
        }
        this.lv_ShowProcess.EndUpdate();
    }
    public void F_AddProcess(ListView listview_t, string str_t_e_path, string str_t_e_name, int i_t_e_ImageIndex)
    {
        try
        {
            ListViewItem lvi = new ListViewItem();
            lvi.ImageIndex = i_t_e_ImageIndex;
            lvi.Text = str_t_e_name;
            lvi.Name = str_t_e_path;
            listview_t.Items.Add(lvi);
        }
        catch
        {
        }
    }
    public void F_Run(string str_t_e_path)
    {
        Process.Start(str_t_e_path);
    }
    public string F_GetInfo()
    {
        string str_t_e_allinfo = "";
        for (int i = 0; i < lv_ShowProcess.Items.Count; i++)
        {
            str_t_e_allinfo += "path=" + lv_ShowProcess.Items[i].Name + "\r\nname=" + lv_ShowProcess.Items[i].Text + "\r\n";
        }
        return str_t_e_allinfo;
    }
    private void 启动ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        F_Run(lv_ShowProcess.SelectedItems[0].Name);
    }

    private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        il_a_e_ProcessIcon.Images.RemoveAt(lv_ShowProcess.SelectedItems[0].Index);
        lv_ShowProcess.SelectedItems[0].Remove();
    }

    private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        FC_a_E_FileAction.F_E_WriterDataByString(F_GetInfo(), "\\config\\set.inf");
        MessageBox.Show("参数保存完毕");
    }

    public string str_a_e_ReName = "";
    private void 重命名ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        frm_input frm_t_GetReName = new frm_input();
        frm_t_GetReName.str_a_e_ReName = lv_ShowProcess.SelectedItems[0].Text;
        frm_t_GetReName.ShowDialog();
        if (DialogResult.OK == frm_t_GetReName.DialogResult)
        {
            if (frm_t_GetReName.str_a_e_ReName != "")
            {
                lv_ShowProcess.SelectedItems[0].Text = frm_t_GetReName.str_a_e_ReName;
            }
        }
        frm_t_GetReName.Dispose();
    }

    private void 添加程序ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        OpenFileDialog dialog = new OpenFileDialog();
        dialog.Title = "请选择文件夹";
        dialog.Filter = "可执行文件(*.exe)|*.exe";
        if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            string file = dialog.FileName;

            Icon icon = System.Drawing.Icon.ExtractAssociatedIcon(file);
            il_a_e_ProcessIcon.Images.Add(icon);

            F_AddProcess(lv_ShowProcess, file, Path.GetFileNameWithoutExtension(file), lv_ShowProcess.Items.Count);
        }
    }

    private void frm_main_FormClosing(object sender, FormClosingEventArgs e)
    {
        FC_a_E_FileAction.F_E_WriterDataByString(F_GetInfo(), "\\config\\set.inf");
    }

    private void lv_ShowProcess_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.F2)
        {
            if (lv_ShowProcess.SelectedIndices.Count > 0)
            {
                frm_input frm_t_GetReName = new frm_input();
                frm_t_GetReName.str_a_e_ReName = lv_ShowProcess.SelectedItems[0].Text;
                frm_t_GetReName.ShowDialog();
                if (DialogResult.OK == frm_t_GetReName.DialogResult)
                {
                    if (frm_t_GetReName.str_a_e_ReName != "")
                    {
                        lv_ShowProcess.SelectedItems[0].Text = frm_t_GetReName.str_a_e_ReName;
                    }
                }
                frm_t_GetReName.Dispose();
            }
        }
    }
}

}
========华丽分界线=======
frm_input.cs
========华丽分界线=======
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 运行其他程序
{
public partial class frm_input : Form
{
public frm_input()
{
InitializeComponent();
}
public string str_a_e_ReName = “”;
private void but_sure_Click(object sender, EventArgs e)
{
str_a_e_ReName = txt_rename.Text;
this.DialogResult = DialogResult.OK;
this.Close();
}

    private void frm_input_Load(object sender, EventArgs e)
    {
        txt_rename.Text = str_a_e_ReName;
    }

    private void txt_rename_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
            str_a_e_ReName = txt_rename.Text;
            this.DialogResult = DialogResult.OK;
            this.Close();
        }
    }
}

}

========华丽分界线=======
DirectoryCommon.cs(其实可以不要)
========华丽分界线=======
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace BaseCommon
{
public class DirectoryCommon
{

    public Boolean F_E_DirectoryExists(string str_t_e_Path)
    {
        return Directory.Exists(Environment.CurrentDirectory + str_t_e_Path);
    }

    public string F_E_DirectoryCreat(string str_t_e_Path)
    {
        try
        {
            Directory.CreateDirectory(Environment.CurrentDirectory + str_t_e_Path);
            return "ok";
        }
        catch (Exception ex)
        {
            return "文件夹创建失败:" + ex.Message.ToString();
        }
    }
}

}

========华丽分界线=======
FileCommon.cs
========华丽分界线=======
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace BaseCommon
{
public class FileCommon
{

    public string F_E_WriterDataByString(string str_t_E_WriteData, string str_t_E_FilePath)
    {
        try
        {

            using (StreamWriter sw_t_E_WriteData = new StreamWriter(Environment.CurrentDirectory + "\\" + str_t_E_FilePath, false, Encoding.Default))
            {
                sw_t_E_WriteData.WriteLine(str_t_E_WriteData);
                sw_t_E_WriteData.Close();
                return "OK";
            }
        }
        catch (Exception ex)
        {
            return "string方式写入信息发生错误:" + ex.Message;

        }
    }
    public string F_E_ReadDataByString(string str_t_E_FilePath)
    {
        try
        {
            using (StreamReader sr_t_E_ReadData = new StreamReader(Environment.CurrentDirectory + "\\" + str_t_E_FilePath, Encoding.Default))
            {
                return sr_t_E_ReadData.ReadToEnd(); ;
            }
        }
        catch (Exception ex)
        {
            return "string方式读取信息发生错误:" + ex.Message;

        }
    }
    public Boolean F_E_FileExists(string str_t_E_FilePath)
    {
        return File.Exists(Environment.CurrentDirectory + str_t_E_FilePath);
    }
    public string F_E_FileCreate(string str_t_E_FilePath)
    {
        try
        {
            File.Create(Environment.CurrentDirectory + str_t_E_FilePath).Dispose();
            return "ok";
        }
        catch (Exception ex)
        {
            return "文件创建失败:" + ex.Message;
        }

    }
}

}

相关标签: C#源码分享