Esempio n. 1
0
        public List <string> getRunList()
        {
            List <string> runList = new List <string>();

            vmFile vfile = new vmFile();

            System.Diagnostics.Process p = new System.Diagnostics.Process();
            p.StartInfo.FileName               = vfile.getVmrunPath() + "\\vmrun.exe";
            p.StartInfo.Arguments              = "list";
            p.StartInfo.UseShellExecute        = false;
            p.StartInfo.RedirectStandardInput  = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError  = true;
            p.StartInfo.CreateNoWindow         = true;

            p.Start();

            p.StandardInput.AutoFlush = true;

            StreamReader reader = p.StandardOutput;
            string       output = reader.ReadLine();

            while (!reader.EndOfStream)
            {
                output = reader.ReadLine();
                runList.Add(output);
            }

            p.WaitForExit();
            p.Close();

            return(runList);
        }
Esempio n. 2
0
        /*
         *  0: startup
         *  1: suspend
         *  2: stop
         */
        public void control(int op, string path)
        {
            string args = null;

            if (op == 0)
            {
                args = "start " + "\"" + path + "\"" + " nogui";
            }
            else if (op == 1)
            {
                args = "suspend " + "\"" + path + "\"";
            }
            else if (op == 2)
            {
                args = "stop " + "\"" + path + "\"";
            }

            vmFile vfile = new vmFile();

            System.Diagnostics.Process p = new System.Diagnostics.Process();
            p.StartInfo.FileName               = vfile.getVmrunPath() + "\\vmrun.exe";
            p.StartInfo.Arguments              = args;
            p.StartInfo.UseShellExecute        = false;
            p.StartInfo.RedirectStandardInput  = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError  = true;
            p.StartInfo.CreateNoWindow         = true;

            p.Start();

            //p.WaitForExit();
            p.Close();
        }
Esempio n. 3
0
        //“导出窗口”的按钮事件
        private void export_Click(object sender, EventArgs e)
        {
            try
            {
                SaveFileDialog oF = new SaveFileDialog();
                //设置文件类型
                oF.Filter = "数据文件(*.txt) | *.txt|数据文件(*.csv) | *.csv |数据文件(*.bak) | *.bak";
                //文件的显示顺序
                oF.FilterIndex = 1;
                //保存对话框是否记忆上次打开的目录
                oF.RestoreDirectory = true;
                //设置默认的文件名
                oF.FileName = "虚拟机备份";

                DialogResult res;
                res = oF.ShowDialog();
                if (res != DialogResult.OK)
                {
                    return;
                }

                string localFilePath = oF.FileName.ToString();

                vmFile vfile = new vmFile();
                vfile.exportVmHostList(localFilePath);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Exception:\r\n" + ex.Message.ToString());
            }
        }
Esempio n. 4
0
        //“编辑窗口”的“确定”按钮事件
        private void editYesButton_Click(object sender, EventArgs e)
        {
            Button  btn   = (Button)sender;
            Form    fm    = (Form)btn.Parent;
            Label   idL   = (Label)fm.Controls[fm.Controls.IndexOfKey("id")];
            TextBox nameB = (TextBox)fm.Controls[fm.Controls.IndexOfKey("name")];
            TextBox pathB = (TextBox)fm.Controls[fm.Controls.IndexOfKey("path")];
            Label   warnL = (Label)fm.Controls[fm.Controls.IndexOfKey("warn")];

            //窗口类型:添加?编辑?
            bool flag = (idL.Text.Length == 0);

            string id   = idL.Text.Trim();
            string name = nameB.Text.Trim();
            string path = pathB.Text.Trim();

            vmFile vfile = new vmFile();

            if (name.Length == 0)
            {
                warnL.Text = "虚拟机名称不能为空";
                return;
            }

            if (path.Length == 0)
            {
                warnL.Text = "虚拟机位置不能为空";
                return;
            }

            if (flag && vfile.isVmHostExist(id))
            {
                warnL.Text = "虚拟机[" + name + "]已经存在";
                return;
            }

            vmFile.vmHost host = new vmFile.vmHost();
            host.id   = id;
            host.name = name;
            host.path = path;

            if (flag)
            {
                host.id = System.Guid.NewGuid().ToString();
                vfile.addVmHost(host);
            }
            else
            {
                vfile.editVmHost(host);
            }

            fm.Close();
        }
Esempio n. 5
0
        //“导入窗口”的yes按钮事件
        private void importYesButton_Click(object sender, EventArgs e)
        {
            Form         iForm = (Form)((Button)sender).Parent;
            DataGridView iView = (DataGridView)((Panel)iForm.Controls["iPanal"]).Controls["iView"];
            vmFile       vfile = new vmFile();

            for (int i = 0; i < iView.Rows.Count; i++)
            {
                vmFile.vmHost host = new vmFile.vmHost();
                host.id   = System.Guid.NewGuid().ToString();
                host.name = iView.Rows[i].Cells["name"].Value.ToString();
                host.path = iView.Rows[i].Cells["path"].Value.ToString();
                vfile.addVmHost(host);
                iForm.Close();
                refreshAll_Click(sender, e);
            }
        }
Esempio n. 6
0
        //删除选中的虚拟机
        private void deleteSelected_Click(object sender, EventArgs e)
        {
            Vm            vHost = new Vm();
            List <string> hList = new List <string>();
            string        hosts = "确定要删除以下虚拟机?";

            for (int i = 0; i < this.dataGridView.Rows.Count; i++)
            {
                DataGridViewCheckBoxCell checkCell = (DataGridViewCheckBoxCell)this.dataGridView.Rows[i].Cells["check"];
                if (Convert.ToBoolean(checkCell.Value))
                {
                    vmFile vfile = new vmFile();
                    hList.Add(this.dataGridView.Rows[i].Cells["id"].Value.ToString());
                    hosts = hosts + "\r\n" + this.dataGridView.Rows[i].Cells["id"].Value.ToString();
                }
            }

            if (hList.Count > 0)
            {
                DialogResult res;

                res = MessageBox.Show(hosts, "删除", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                //MessageBox.Show(res.ToString());
                if (res == DialogResult.OK)
                {
                    vmFile vfile = new vmFile();
                    foreach (string n in hList)
                    {
                        vfile.delVmHost(n);
                    }
                }
                else
                {
                    return;
                }
            }
            else
            {
                MessageBox.Show("没有选中任何虚拟机");
            }
            refreshAll_Click(sender, e);
        }
Esempio n. 7
0
        //“设置窗口”的“确定”按钮事件
        private void setupYesButton_Click(object sender, EventArgs e)
        {
            Button  btn   = (Button)sender;
            Form    fm    = (Form)btn.Parent;
            TextBox pathB = (TextBox)fm.Controls[fm.Controls.IndexOfKey("path")];
            Label   warnL = (Label)fm.Controls[fm.Controls.IndexOfKey("warn")];

            string path = pathB.Text.Trim();

            if (string.IsNullOrEmpty(path))
            {
                warnL.Text = "VMware路径不能为空";
                return;
            }

            vmFile vfile = new vmFile();

            vfile.setupVmrunPath(path);

            fm.Close();
        }
Esempio n. 8
0
        public List <vmState> getAllVmState()
        {
            List <vmState>       list       = new List <vmState>();
            vmFile               vFile      = new vmFile();
            List <string>        runList    = getRunList();          //正在运行的虚拟机
            List <vmFile.vmHost> vmHostList = vFile.getVmHostList(); //配置的虚拟机

            foreach (vmFile.vmHost vh in vmHostList)
            {
                vmState vs = new vmState();
                vs.id   = vh.id;
                vs.name = vh.name;
                vs.path = vh.path;

                /*
                 * 获取虚拟机名字和状态:
                 * - vmx文件不存在,则虚拟机不存在
                 * - vmx文件中没有checkpoint.vmState字段或为空,则处于开机或关机状态,先标记为关机状态,最后如果有开机的再更新一下
                 * - vmx文件的checkpoint.vmState字段为*.vmss,则处于挂起状态
                 * - 如果vmx发生io错误,可虚拟机正在启动,无法读取该文件
                 */
                if (!File.Exists(vh.path))
                {
                    vs.displayName = "不存在";
                    vs.state       = 4;
                }
                else
                {
                    try
                    {
                        Dictionary <string, string> conf = File.ReadAllLines(vh.path)
                                                           .Select(line => line.Split(new[] { '=' }, 2, StringSplitOptions.RemoveEmptyEntries))
                                                           .Where(split => split.Length == 2)
                                                           .GroupBy(split => split[0].Trim())
                                                           .ToDictionary(group => group.Key.Trim(), group => group.Last().Last().Trim().TrimEnd('"').TrimStart('"'));

                        vs.displayName = conf["displayName"];

                        if (conf.ContainsKey("checkpoint.vmState") && conf["checkpoint.vmState"].EndsWith(".vmss"))
                        {
                            vs.state = 2;
                        }
                        else
                        {
                            vs.state = 3;
                        }
                    }
                    catch (IOException)
                    {
                        vs.displayName = "启动中";
                        vs.state       = 1;
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                }

                foreach (string rp in runList)
                {
                    if (vs.path.Equals(rp))
                    {
                        vs.state = 0;
                    }
                }

                list.Add(vs);
            }

            return(list);
        }
Esempio n. 9
0
        //设置窗口
        private void setup_Click(object sender, EventArgs e)
        {
            /*
             * 控件列表
             * - Form:主窗体
             *   - Label:顶部警告框,默认为空
             *   - Lable+TextBox:VMware路径标签和输入框
             *   - Button:底部确定按钮
             *   - Button:底部取消按钮
             */
            Form    cForm = new Form();
            Label   warnL = new Label();
            Label   pathL = new Label();
            TextBox pathB = new TextBox();
            Button  yesB  = new Button();
            Button  noB   = new Button();

            //主窗体
            cForm.Text            = "编辑";
            cForm.StartPosition   = FormStartPosition.CenterScreen;
            cForm.Width           = 400;
            cForm.Height          = 250;
            cForm.FormBorderStyle = FormBorderStyle.FixedSingle;
            cForm.MaximizeBox     = false;

            //顶部警告框
            warnL.Name      = "warn";
            warnL.Width     = 380;
            warnL.ForeColor = System.Drawing.Color.Red;
            warnL.Location  = new Point(10, 10);

            /*
             * VMware Workstation位置和输入框
             *                    _____________
             *   VMware安装路径 |____________|
             */
            pathL.Text     = "VMware安装路径";
            pathL.Width    = 90;
            pathL.Location = new Point(50, 70);

            pathB.Name     = "path";
            pathB.Width    = 210;
            pathB.Location = new Point(140, 70);
            vmFile vfile = new vmFile();

            pathB.Text = vfile.getVmrunPath();;

            //底部确定按钮
            yesB.Text     = "确定";
            yesB.Width    = 50;
            yesB.Location = new Point(125, 150);
            yesB.Click   += setupYesButton_Click;

            //底部取消按钮
            noB.Text     = "取消";
            noB.Width    = 50;
            noB.Location = new Point(225, 150);
            noB.Click   += setupNoButton_Click;

            //加载控件和窗口
            cForm.Controls.Add(warnL);
            cForm.Controls.Add(pathL);
            cForm.Controls.Add(pathB);
            cForm.Controls.Add(yesB);
            cForm.Controls.Add(noB);
            cForm.ShowDialog();
        }