首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过ASP.NET基于DOS的打印

通过ASP.NET基于DOS的打印
EN

Stack Overflow用户
提问于 2009-04-25 06:31:44
回答 2查看 3.7K关注 0票数 2

我的情况是这样的:

我正在服务器上生成一个文本文件形式的报告,需要在点阵打印机上使用DOS模式进行打印。我想避免Windows打印,因为它太慢了。在ASP.NET中有没有一种方法可以进行基于DOS的打印,因为它最适合于点阵打印机。我已经搜索了网络,但没有找到任何解决方案或指示。有没有人有他们可能已经实现或偶然发现的任何指针/解决方案。

此应用程序是一个基于Web的应用程序。

谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2009-04-25 07:00:13

如果我没理解错的话,一种选择是执行一个批处理文件,该文件将从ASP.NET执行实际的打印。

代码语言:javascript
复制
// 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>")));
票数 4
EN

Stack Overflow用户

发布于 2009-04-25 12:25:46

来自BobbyShaftoe的答案是正确的。这是它的一个书生气的版本:

代码语言:javascript
复制
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>")));
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/788363

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档