码迷,mamicode.com
首页 > 其他好文 > 详细

GHO2VMDK转换工具分享含VS2010源码

时间:2017-09-02 20:45:20      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:catch   send   apr   操作   private   director   generic   虚拟   命令行参数   

平常经常用到虚拟机,每次从gho转换为vmdk时都要输入cmd代码,觉得麻烦,自己动手做了个gho2vmdk转换工具,集成ghost32.exe文件,可以一键转换,省时省事。运行时会将ghost32.exe保存到Program FIles文件夹里,运行完自动删除ghost32.exe。觉得还不错,在此分享一下,有什么好的建议,欢迎反馈。代码贴上。需要完整工程的请留言邮箱。开发工具为VS2010,没用任何第三方插件。觉得有帮助举手点个推荐。

 

程序下载地址:链接:http://pan.baidu.com/s/1bp2HBw7 密码:jpw4

 

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using GHO2VMDK转换工具.Properties;

namespace GHO2VMDK转换工具
{
    public partial class Form1 : Form
    {
        private string _ghost32ExeFullName;

        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Form1_Load
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Load(object sender, EventArgs e)
        {
            //启用拖放
            txtGhoFullName.AllowDrop = true;
            //在拖入边界时发生
            txtGhoFullName.DragEnter += (s1, e1) =>
            {
                if (e1.Data.GetDataPresent(DataFormats.FileDrop))
                {
                    e1.Effect = DragDropEffects.Link;
                }
                else
                {
                    e1.Effect = DragDropEffects.None;
                }
            };

            //在拖放完成时发生
            txtGhoFullName.DragDrop += (s1, e1) =>
            {
                //攻取gho文件路径
                string tmpPath = ((Array) (e1.Data.GetData(DataFormats.FileDrop))).GetValue(0).ToString();
                string extension = Path.GetExtension(tmpPath);
                if (extension.ToLower() == ".gho")
                    txtGhoFullName.Text = tmpPath;
                else
                    MessageBox.Show("请拖放GHO文件!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            };
        }

        /// <summary>
        /// txtGhoFullName_TextChanged
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtGhoFullName_TextChanged(object sender, EventArgs e)
        {
            //根据gho文件路径,自动设置vmdk文件默认保存路径
            string ghoFullName = txtGhoFullName.Text;
            string directoryName = Path.GetDirectoryName(ghoFullName);
            string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(ghoFullName);
            if (directoryName != null)
            {
                string vmdkFullName = Path.Combine(directoryName, fileNameWithoutExtension + ".vmdk");
                txtVmdkFullName.Text = vmdkFullName;
            }
        }

        /// <summary>
        /// btnStartConvert_Click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnStartConvert_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("是否开始将GHO文件转换为VMDK文件?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                //获取ProgramFiles路径
                string path = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
                //设置ghost32.exe文件临时保存路径
                _ghost32ExeFullName = Path.Combine(path, "ghost32.exe");
                if (!File.Exists(_ghost32ExeFullName))
                {
                    //从资源文件读取ghost32.exe并保存到_ghost32ExeFullName路径
                    FileStream str = new FileStream(_ghost32ExeFullName, FileMode.OpenOrCreate);
                    str.Write(Resources.Ghost32, 0, Resources.Ghost32.Length);
                    str.Close();
                }

                //设置ghost32.exe运行参数
                string cmdText = string.Format("-clone,mode=restore,src=\"{0}\",dst=\"{1}\" -batch -sure",
                    txtGhoFullName.Text, txtVmdkFullName.Text);
                //隐蔽窗口
                WindowState = FormWindowState.Minimized;
                Hide();
                //运行ghost32.exe
                RunCmd(_ghost32ExeFullName, cmdText);
                Show();
                //显示窗口
                WindowState = FormWindowState.Normal;
                //删除ghost32.exe
                if (File.Exists(_ghost32ExeFullName))
                    File.Delete(_ghost32ExeFullName);

                MessageBox.Show("操作完成!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        /// <summary>
        ///     运行cmd命令
        ///     不会显示命令窗口
        /// </summary>
        /// <param name="cmdExe">指定应用程序的完整路径</param>
        /// <param name="cmdStr">执行命令行参数</param>
        private static bool RunCmd(string cmdExe, string cmdStr)
        {
            bool result = false;
            try
            {
                using (Process myPro = new Process())
                {
                    //指定启动进程是调用的应用程序和命令行参数
                    ProcessStartInfo psi = new ProcessStartInfo(cmdExe, cmdStr);
                    psi.UseShellExecute = false;
                    psi.RedirectStandardInput = true;
                    psi.RedirectStandardOutput = true;
                    psi.RedirectStandardError = true;
                    psi.CreateNoWindow = true;

                    myPro.StartInfo = psi;
                    myPro.Start();
                    myPro.WaitForExit();
                    result = true;
                }
            }
            catch
            {
                result = false;
            }
            return result;
        }

        /// <summary>
        ///     运行cmd命令
        ///     不显示命令窗口
        /// </summary>
        /// <param name="cmdExe">指定应用程序的完整路径</param>
        /// <param name="cmdStr">执行命令行参数</param>
        private static bool RunCmd2(string cmdExe, string cmdStr)
        {
            bool result = false;
            try
            {
                using (Process myPro = new Process())
                {
                    myPro.StartInfo.FileName = "cmd.exe";
                    myPro.StartInfo.UseShellExecute = false;
                    myPro.StartInfo.RedirectStandardInput = true;
                    myPro.StartInfo.RedirectStandardOutput = true;
                    myPro.StartInfo.RedirectStandardError = true;
                    myPro.StartInfo.CreateNoWindow = true;
                    myPro.Start();
                    //如果调用程序路径中有空格时,cmd命令执行失败,可以用双引号括起来 ,在这里两个引号表示一个引号(转义)
                    string str = string.Format(@"""{0}"" {1} {2}", cmdExe, cmdStr, "&exit");

                    myPro.StandardInput.WriteLine(str);
                    myPro.StandardInput.AutoFlush = true;
                    myPro.WaitForExit();

                    result = true;
                }
            }
            catch
            {
                result = false;
            }
            return result;
        }

        private void chbTopMost_CheckedChanged(object sender, EventArgs e)
        {
            //窗口置顶
            TopMost = chbTopMost.Checked;
        }

        private void btnBrowseGhoFile_Click(object sender, EventArgs e)
        {
            //浏览gho文件
            string ghoFullName = txtGhoFullName.Text;

            OpenFileDialog f = new OpenFileDialog
            {
                Title = "浏览GHO文件...",
                Filter = "GHO文件(*.gho)|*.gho",
                FileName = ghoFullName,
                InitialDirectory = ghoFullName
            };
            if (f.ShowDialog() == DialogResult.OK)
            {
                txtGhoFullName.Text = f.FileName;
            }
        }

        private void btnBrowseVmdkFile_Click(object sender, EventArgs e)
        {
            //浏览VMDK文件
            string vmdkFullName = txtVmdkFullName.Text;
            SaveFileDialog f = new SaveFileDialog
            {
                Title = "设置VMDK文件保存路径...",
                Filter = "VMDK文件(*.vmdk)|*.vmdk",
                FileName = vmdkFullName,
                InitialDirectory = vmdkFullName
            };
            if (f.ShowDialog() == DialogResult.OK)
            {
                txtVmdkFullName.Text = f.FileName;
            }
        }
    }
}

 

GHO2VMDK转换工具分享含VS2010源码

标签:catch   send   apr   操作   private   director   generic   虚拟   命令行参数   

原文地址:http://www.cnblogs.com/InfoStudio/p/7467504.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!