实现效果:
知识运用:
Process 类的 StartInfo 属性 // 获取或设置要传递给当前进程的 Start 方法的属性
public ProcessStartInfo StartInfo { get; set; }
Process 类的 StandardInput 属性 // 只读属性 获取拥有写入应用程序输入的流
public StreamWrite StandardInput { get; set; }
Process 类的 Start 方法 // 启动当前进程资源
public bool Start() // 返回进程是否启动成功
实现代码:
- private void button1_Click(object sender, EventArgs e)
- {
- SaveFileDialog save = new SaveFileDialog();
- save.FileName = "Backup";
- save.Filter = "*.reg|*.reg";
- save.ShowDialog();
- textBox1.Text = save.FileName;
- }
- private void button2_Click(object sender, EventArgs e)
- {
- try
- {
- Process ps = new Process();
- ps.StartInfo.FileName = "cmd.exe";
- ps.StartInfo.CreateNoWindow = true; // 设置启动该进程而不创建包含它的新窗口
- ps.StartInfo.UseShellExecute = false; // 设置直接从可执行文件创建进程
- ps.StartInfo.RedirectStandardInput = true;
- ps.StartInfo.RedirectStandardOutput = true;
- ps.StartInfo.RedirectStandardError = true;
- ps.Start();
- ps.StandardInput.WriteLine("regedit /e"+textBox1.Text);
- MessageBox.Show("注册表已备份成功!");
- }
- catch (Exception ex)
- { MessageBox.Show(ex.Message); }
- }
备份注册表
来源: http://www.bubuko.com/infodetail-2935741.html