views:

900

answers:

2

Well my situation is like this:

I am generating a report as a text file at the server which needs to be printed using DOS mode on a dot matrix printer. I want to avoid Windows printing because it would be too slow. Is there a way in ASP.NET through which I can carry out DOS based printing as it is best suited for Dot matrix printers. I have scoured the net but could not come across any solution or pointers. Does any body have any pointers/solutions which they might have implemented or stumbled across.

This application is a Web based application.

Thanx.

+3  A: 

If I understand you right, one option is to execute a batch file that would do the actual printing from ASP.NET. From here: (Obviously, you can omit some of the code writing the output to the page)

// Get the full file path
string strFilePath = “c:\\temp\\test.bat”;

// Create the ProcessInfo object
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo("cmd.exe");
psi.UseShellExecute = false; 
psi.RedirectStandardOutput = true;
psi.RedirectStandardInput = true;
psi.RedirectStandardError = true;
psi.WorkingDirectory = “c:\\temp\\“;

// Start the process
System.Diagnostics.Process proc = System.Diagnostics.Process.Start(psi);


// Open the batch file for reading
System.IO.StreamReader strm = System.IO.File.OpenText(strFilePath); 

// Attach the output for reading
System.IO.StreamReader sOut = proc.StandardOutput;

// Attach the in for writing
System.IO.StreamWriter sIn = proc.StandardInput;


// Write each line of the batch file to standard input
while(strm.Peek() != -1)
{
  sIn.WriteLine(strm.ReadLine());
}

strm.Close();

// Exit CMD.EXE
string stEchoFmt = "# {0} run successfully. Exiting";

sIn.WriteLine(String.Format(stEchoFmt, strFilePath));
sIn.WriteLine("EXIT");

// Close the process
proc.Close();

// Read the sOut to a string.
string results = sOut.ReadToEnd().Trim();


// Close the io Streams;
sIn.Close(); 
sOut.Close();


// Write out the results.
string fmtStdOut = "<font face=courier size=0>{0}</font>";
this.Response.Write(String.Format(fmtStdOut,results.Replace(System.Environment.NewLine, "<br>")));
BobbyShaftoe
that's some serious hacking you got going on there man! good job. I would have tossed it into a queue and figured out what to do with it later. lol
Chad Grant
A: 

The answer from BobbyShaftoe is correct. Here's a pedantic version of it:

public static void CreateProcess(string strFilePath)
{
    // Create the ProcessInfo object
    var psi = new ProcessStartInfo("cmd.exe")
                  {
                      UseShellExecute = false,
                      RedirectStandardOutput = true,
                      RedirectStandardInput = true,
                      RedirectStandardError = true,
                      WorkingDirectory = "c:\\temp\\"
                  };

    // Start the process
    using (var proc = Process.Start(psi))
    {
        // Attach the in for writing
        var sIn = proc.StandardInput;

        using (var strm = File.OpenText(strFilePath))
        {
            // Write each line of the batch file to standard input
            while (strm.Peek() != -1)
            {
                sIn.WriteLine(strm.ReadLine());
            }
        }

        // Exit CMD.EXE

        sIn.WriteLine(String.Format("# {0} run successfully. Exiting", strFilePath));
        sIn.WriteLine("EXIT");

        // Read the sOut to a string.
        var results = proc.StandardOutput.ReadToEnd().Trim();

        // Write out the results.
        string fmtStdOut = "<font face=courier size=0>{0}</font>";
        this.Response.Write(String.Format(fmtStdOut,results.Replace(System.Environment.NewLine, "<br>")));
    }
}
John Saunders