GHO2VMDK轉換工具分享含VS2010源碼
阿新 • • 發佈:2017-09-02
change batch ogr combine 參數 false 失敗 rms windows
平常經常用到虛擬機,每次從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源碼