[DllImport("user32.dll")] static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
Process p = new Process(); p.StartInfo.FileName = "format"; p.StartInfo.Arguments = // ... Je te laisse mettre tes arguments p.StartInfo.RedirectStandardOutput = true; p.StartInfo.UseShellExecute = false; p.Start();
string s; do { Thread.Sleep(10); s = p.StandardOutput.ReadLine(); } while (s != "Lancer le formatage (O/N)?"); // Vérifie que la question est bien celle-ci, au caractère près.
IntPtr ptr = p.MainWindowHandle; ShowWindow(ptr, 1); SendKeys.SendWait("O"); p.WaitForExit();
//Utilisation de la class ProcessStartInfo ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.CreateNoWindow = false; startInfo.UseShellExecute = true; startInfo.FileName = "format"; startInfo.WindowStyle = ProcessWindowStyle.Hidden; startInfo.Arguments = myIni.GetValue("CléUSB","cheminClé") + " " +"/q" + " " + "/y"; try { // Démarrez le processus avec les infos que nous avons spécifié. using (Process exeProcess = Process.Start(startInfo)) { exeProcess.WaitForExit(); } } catch { MessageBox.Show("ERREUR"); // Log error. }
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionSystem.Diagnostics.Process.Start(ligneDeCommande);
public enum SHFormatFlags : uint { SHFMT_ID_DEFAULT = 0xFFFF, SHFMT_OPT_FULL = 0x1, SHFMT_OPT_SYSONLY = 0x2, SHFMT_ERROR = 0xFFFFFFFF, SHFMT_CANCEL = 0xFFFFFFFE, SHFMT_NOFORMAT = 0xFFFFFFD, } public const uint SHFMT_ERROR = 0xFFFFFFFF; public const uint SHFMT_CANCEL = 0xFFFFFFFE; public const uint SHFMT_NOFORMAT = 0xFFFFFFD; [DllImport("shell32.dll")] static extern uint SHFormatDrive(IntPtr hwnd, uint drive, uint fmtID, uint options); /// <summary> /// Permit to check the result of the format call /// Throw Exception with a detailed message /// </summary> /// public static void CheckFormatResult(uint shFormatResult) { if (shFormatResult == SHFMT_ERROR) throw new Exception("An error occurred during the format. This does not indicate that the drive is unformattable."); if (shFormatResult == SHFMT_CANCEL) throw new OperationCanceledException("The format was canceled."); if (shFormatResult == SHFMT_NOFORMAT) throw new IOException("The drive cannot be formatted."); //we can exit normally return; } public void formatUSB() { uint result = SHFormatDrive(this.Handle, 4, // formatting E: (uint)SHFormatFlags.SHFMT_ID_DEFAULT, 0); // full format of E: if (result == (uint)SHFormatFlags.SHFMT_ERROR) MessageBox.Show("Unable to format the drive"); } }