Form1.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Diagnostics;
  6. using System.Drawing;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12. using System.Diagnostics;
  13. namespace 空白文件生成器
  14. {
  15. public partial class Form1 : Form
  16. {
  17. long size = 0;
  18. public Form1()
  19. {
  20. InitializeComponent();
  21. label7.Text = trackBar1.Value.ToString();
  22. numericUpDown1.Increment = trackBar1.Value;
  23. }
  24. private void 关于ToolStripMenuItem_Click(object sender, EventArgs e)
  25. {
  26. Form form = new AboutBox1();
  27. form.ShowDialog();
  28. }
  29. private void trackBar1_ValueChanged(object sender, EventArgs e)
  30. {
  31. label7.Text = trackBar1.Value.ToString();
  32. numericUpDown1.Increment = trackBar1.Value;
  33. }
  34. private void button1_Click(object sender, EventArgs e)
  35. {
  36. if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
  37. {
  38. textBox1.Text = folderBrowserDialog1.SelectedPath;
  39. }
  40. }
  41. private void button2_Click(object sender, EventArgs e)
  42. {
  43. label555:
  44. if (Directory.Exists(textBox1.Text))
  45. {
  46. if (label2.Text != "")
  47. {
  48. if (comboBox1.Text != "")
  49. {
  50. if (numericUpDown1.Value > 0)
  51. {
  52. if (comboBox2.Text != "")
  53. {
  54. string path=textBox1.Text+@"\"+textBox2.Text+"."+comboBox1.Text;
  55. if (comboBox2.Text == "B")
  56. {
  57. size = (int)numericUpDown1.Value;
  58. }
  59. if (comboBox2.Text == "KB")
  60. {
  61. size = (int)numericUpDown1.Value*1024;
  62. }
  63. if (comboBox2.Text == "MB")
  64. {
  65. size = (int)numericUpDown1.Value * 1024 * 1024;
  66. }
  67. if (comboBox2.Text == "GB")
  68. {
  69. size = (long)numericUpDown1.Value * 1024 * 1024 * 1024;
  70. }
  71. if (MessageBox.Show("文件地址:"+path+ "\r\n文件大小:" + size.ToString(), "确认信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
  72. {
  73. Process process = new Process();
  74. process.StartInfo.UseShellExecute = false; //是否使用操作系统shell启动
  75. process.StartInfo.CreateNoWindow = true; //是否在新窗口中启动该进程的值 (不显示程序窗口)
  76. process.StartInfo.RedirectStandardInput = true; // 接受来自调用程序的输入信息
  77. process.StartInfo.RedirectStandardOutput = true; // 由调用程序获取输出信息
  78. process.StartInfo.RedirectStandardError = true; //重定向标准错误输出
  79. process.StartInfo.FileName = "cmd.exe";
  80. process.Start(); // 启动程序
  81. process.StandardInput.WriteLine("fsutil file createnew " + path + " " + size); //向cmd窗口发送输入信息
  82. process.StandardInput.AutoFlush = true;
  83. // 前面一个命令不管是否执行成功都执行后面(exit)命令,如果不执行exit命令,后面调用ReadToEnd()方法会假死
  84. process.StandardInput.WriteLine("exit");
  85. StreamReader reader = process.StandardOutput;//获取exe处理之后的输出信息
  86. string curLine = reader.ReadLine(); //获取错误信息到error
  87. int line = 0;
  88. while (!reader.EndOfStream)
  89. {
  90. if (!string.IsNullOrEmpty(curLine))
  91. {
  92. line++;
  93. if (line == 4)
  94. {
  95. MessageBox.Show(curLine, "生成结果", MessageBoxButtons.OK, MessageBoxIcon.Information);
  96. }
  97. }
  98. curLine = reader.ReadLine();
  99. }
  100. reader.Close(); //close进程
  101. process.WaitForExit(); //等待程序执行完退出进程
  102. process.Close();
  103. }
  104. }
  105. else
  106. {
  107. MessageBox.Show("文件大小单位无效,请重新选择文件单位", "生成失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
  108. }
  109. }
  110. else
  111. {
  112. MessageBox.Show("文件大小无效,请重新输入文件大小", "生成失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
  113. }
  114. }
  115. else
  116. {
  117. MessageBox.Show("文件扩展名无效,请重新输入或选择扩展名", "生成失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
  118. }
  119. }
  120. else
  121. {
  122. MessageBox.Show("文件名无效,请重新输入文件名", "生成失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
  123. }
  124. }
  125. else
  126. {
  127. if (textBox1.Text != "")
  128. {
  129. DirectoryInfo directoryInfo = new DirectoryInfo(textBox1.Text);
  130. directoryInfo.Create();
  131. goto label555;
  132. }
  133. else
  134. {
  135. MessageBox.Show("位置无效,请重新输入路径或浏览文件夹", "生成失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
  136. }
  137. }
  138. }
  139. }
  140. }