Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Automate a simple Floppy Format

Status
Not open for further replies.

zackiv31

Programmer
May 25, 2006
148
US
Here's what I got, but I dont know how to sendkeys only when input is waiting for something:

Code:
Process proc = new Process();
                    proc.StartInfo.FileName = Environment.GetFolderPath(Environment.SpecialFolder.System).ToString() + @"\format";
                    proc.StartInfo.Arguments = comboBoxF3.SelectedItem.ToString() + "/V:";

                    proc.Start();
                    SendKeys.SendWait("{ENTER}");
//HERE is where I need assistance..
                    SendKeys.SendWait("{n}");
                    SendKeys.SendWait("{ENTER}");

In the above I need to wait where it says "HERE" until the format has finished... it asks if I want to format another (y/n) and so I then want to send the "n" and "ENTER"
T
 
You need to redirect the input buffer and pass in your "\r\n" to that buffer.

Look at p.StartInfo.RedirectStandardInput = true;

You may have to turn UseShellExecute to false;

 
I've tried something like this (trying to work off of JurkMonkeys suggestion... I don't know batch files well at all, and I don't want to require one with my application.

Heres what I got:

Code:
Process p = new Process();
                    p.StartInfo.FileName = Environment.GetFolderPath(Environment.SpecialFolder.System).ToString() + @"\format";
                    p.StartInfo.Arguments = cb.SelectedItem.ToString() + " /V:";
                    p.StartInfo.RedirectStandardInput = true;
                    p.StartInfo.UseShellExecute = false;
                    p.Start();
                    p.StandardInput = new System.IO.StreamWriter(new Stream("\r\nn\r\n"));

Obviously the last part is wrong as it doesn't pass compile, but how should I pass in those arguments to my program?
 
p.StandardInput = new System.IO.StreamWriter(new Stream("\r\nn\r\n"));

What you want to do is something like p.StandardInput.WriteLine(");

or

p.StandardInput.Write("\r\n");

 
In the right direction, but still not working, new code:

Code:
                    Process p = new Process();
                    p.StartInfo.FileName = Environment.GetFolderPath(Environment.SpecialFolder.System).ToString() + @"\format";
                    p.StartInfo.Arguments = cb.SelectedItem.ToString() + " /V:";
                    MessageBox.Show("1");
                    p.StartInfo.RedirectStandardInput = true;
                    MessageBox.Show("2");
                    //p.StartInfo.UseShellExecute = false;
                    MessageBox.Show("3");
                    p.StandardInput.Write("\r\nn\r\n");
                    MessageBox.Show("4");
                    p.Start();

It never shows '4', but I've redirected standard input... ?
 
I tried that before, that's why I moved it.. it gives me a System.ComponentModel.Win32Exception: The system cannot find the file specified.

Code:
                    Process p = new Process();
                    p.StartInfo.FileName = Environment.GetFolderPath(Environment.SpecialFolder.System).ToString() + @"\format";
                    p.StartInfo.Arguments = cb.SelectedItem.ToString() + " /V:";
                    MessageBox.Show("1");
                    p.StartInfo.RedirectStandardInput = true;
                    MessageBox.Show("2");
                    p.StartInfo.UseShellExecute = false;
                    MessageBox.Show("3");
                    //p.StandardInput.Write("\r\nn\r\n");
                    MessageBox.Show("4");
                    p.Start();
                    MessageBox.Show(p.StartInfo.RedirectStandardInput.ToString());
                    p.StandardInput.Write("\r\nn\r\n");

And I put "\r\nn\r\n" because I need to enter the "n" key for no, and hit enter again..

 
OK well here's what I've determined.

If I just have the Filename and Arguments set, and I start it, it launches fine (opens up command prompt with right command).

If I have those plus the redirect and the useshell, and I try to start the process, it crashes with the above Win32Exception..


WTF?
 
Anyone have any ideas on this? I still can't get it to operate correctly.
 
FORMAT a: /AUTOTEST should perform an unattended format without user intervention. so creating a process and using noshellexecute and redirecting the output to a stream reader should be what you want I think.

AUTOTEST info came from here:
 
format and fdisk are not the same... format doesn't register the /autotest option
 
indeed they are different - the format options were listed further down the page - a quick search of goole found loads of similar info but as you rightly pointed out windows doesn't recognise the parameter ...
 
anyone have any ideas on this? I'm just trying to autoformat a floppy from C# any way I can...
 
I finally returned to this problem and was able to initiate the SHFormatDrive through shell32.dll... My new Question is this:

I launch the SHFormatDrive dialog and it appears... but when I get a list of processes, and window handles, it is not listed... is this the behavior of C#? The main form shows up, but the backgroundWorker does not.

I launch it using a backGroundWorker... is that why the dialog box doesn't get a handle? Is there another way to do it?
 
UI things must always be launched from the main (UI) thread. that's how windows works...

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Process didn't work... it would write all lines to standard input, but the \r\n wouldn't get recognized.

Ideally the command line is the best option, but I can't get it to autorun/autoclose..

I have no experience with Windows Batch files, maybe running one of them would work? But I don't see how it's any different then the way I did it in process.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top