Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 斑马赢了';我写文件流时不打印_C#_Printing_Filestream_Zebra Printers_Zpl - Fatal编程技术网

C# 斑马赢了';我写文件流时不打印

C# 斑马赢了';我写文件流时不打印,c#,printing,filestream,zebra-printers,zpl,C#,Printing,Filestream,Zebra Printers,Zpl,我正在尝试使用字节数组将原始zpl数据打印到zebra打印机。当我运行程序时,不会打印任何内容,程序也不会出现问题。它实际打印我要求的内容的唯一时间是,如果我在关闭它之前单步检查我的filestream值,标签将打印出来。有人能帮忙解决这件事的原因吗 更新:对代码进行了建议的调整,这是一个WINapp public bool LabelPrinting(DataTable dt, string LblPath) { if (File.Exists(LblPath))

我正在尝试使用字节数组将原始zpl数据打印到zebra打印机。当我运行程序时,不会打印任何内容,程序也不会出现问题。它实际打印我要求的内容的唯一时间是,如果我在关闭它之前单步检查我的filestream值,标签将打印出来。有人能帮忙解决这件事的原因吗

更新:对代码进行了建议的调整,这是一个WINapp

 public bool LabelPrinting(DataTable dt, string LblPath)
    {
        if (File.Exists(LblPath))
        {
            string strContent = "";
            string comport = cboPort.Text.Trim();
            StreamReader myFile = new StreamReader(LblPath);

            strContent = myFile.ReadToEnd();
            myFile.Close();


            if (strContent.Length == 0)
            {
                return false;
            }
            else
            {
                char[] BigSpaceChars = { Convert.ToChar(0x09) };
                strContent = strContent.Replace(new string(BigSpaceChars), "   ");
                strContent = strContent.Replace("<CPN>", dt.Rows[0]["CPN"].ToString().ToUpper());
                strContent = strContent.Replace("<RACKASSET>", dt.Rows[0]["RACKASSET"].ToString().ToUpper());
                strContent = strContent.Replace("<LOC>", dt.Rows[0]["LOC"].ToString().ToUpper());

                Byte[] buffer = new byte[strContent.Length];
                buffer = System.Text.Encoding.ASCII.GetBytes(strContent);

                SafeFileHandle printer = CreateFile(comport,FileAccess.ReadWrite,0,IntPtr.Zero,FileMode.Open,0,IntPtr.Zero);

                if (printer.IsInvalid == true)
                {
                    return false;
                }



                 using (FileStream com = new FileStream(printer, FileAccess.ReadWrite))
                    {
                        com.Write(buffer, 0, buffer.Length);
                        com.Flush();
                        com.Close();
                    }


                }

               return true;
            }
公共布尔标签打印(数据表dt,字符串LblPath)
{
if(File.Exists(LblPath))
{
字符串strContent=“”;
string comport=cboPort.Text.Trim();
StreamReader myFile=新的StreamReader(LblPath);
strContent=myFile.ReadToEnd();
myFile.Close();
if(strContent.Length==0)
{
返回false;
}
其他的
{
char[]BigSpaceChars={Convert.ToChar(0x09)};
strContent=strContent.Replace(新字符串(BigSpaceChars),“”);
strContent=strContent.Replace(“,dt.Rows[0][“CPN”].ToString().ToUpper());
strContent=strContent.Replace(“,dt.Rows[0][“RACKASSET”].ToString().ToUpper());
strContent=strContent.Replace(“,dt.Rows[0][“LOC”].ToString().ToUpper());
字节[]缓冲区=新字节[strContent.Length];
buffer=System.Text.Encoding.ASCII.GetBytes(strContent);
SafeFileHandle printer=CreateFile(comport,FileAccess.ReadWrite,0,IntPtr.Zero,FileMode.Open,0,IntPtr.Zero);
如果(printer.IsInvalid==true)
{
返回false;
}
使用(FileStream com=newfilestream(打印机,FileAccess.ReadWrite))
{
com.Write(buffer,0,buffer.Length);
com.Flush();
com.Close();
}
}
返回true;
}

您应该将
using
语句与
com
一起使用,因为如果代码在.Close()之前,则流不会关闭抛出异常。或尝试捕获,但确实建议使用。顺便问一下,这是控制台应用程序、类库、winapp等吗?@DieterB我很快放入了一个尝试捕获,发现了一个超时错误,这是因为我在发布此消息之前进行了实验。“com.WriteTimeout”不应该在那里。@EricJ我不知道你说的“使用”是什么意思。我是一个比较新的人,从我观察到的情况来看,文件流似乎正在正确关闭,但打印机没有任何操作。