流不可读

时间:2014-07-12 16:53:34

标签: c# streamwriter

我很抱歉没有更好的方法来解决这个问题。一旦我更好地了解正在发生的事情,我就可以修改。我试图在以下代码块中使用StreamWriter:

 public static bool SendFileToPrinter(string szPrinterName, string fontFileNames)
        {
            // Open the file.
            FileStream fs = new FileStream(fontFileNames, FileMode.Open, FileAccess.Write);
            //Add string to the file
            StreamWriter sw = new StreamWriter(fs);
            sw.WriteLine ("Test");
            // Create a BinaryReader on the file.
            BinaryReader br = new BinaryReader(fs);
            // Dim an array of bytes big enough to hold the file's contents.
            Byte[] bytes = new Byte[fs.Length];
            bool bSuccess = false;
            // Your unmanaged pointer.
            IntPtr pUnmanagedBytes = new IntPtr(0);
            int nLength;
            nLength = Convert.ToInt32(fs.Length);
            // Read the contents of the file into the array.
            bytes = br.ReadBytes(nLength);
            // Allocate some unmanaged memory for those bytes.
            pUnmanagedBytes = Marshal.AllocCoTaskMem(nLength);
            // Copy the managed byte array into the unmanaged array.
            Marshal.Copy(bytes, 0, pUnmanagedBytes, nLength);
            // Send the unmanaged bytes to the printer.
            bSuccess = SendBytesToPrinter(szPrinterName, pUnmanagedBytes, nLength);
            // Free the unmanaged memory that you allocated earlier.
            Marshal.FreeCoTaskMem(pUnmanagedBytes);
            return bSuccess;

        }

当我添加流编写器时,我得到一个" Stream不可读"错误,我不知道为什么。我是新人,所以这可能是初级的。我只想尝试将字符串添加到我发送到打印机的文件中(作为原始PCL)。任何帮助表示赞赏。

2 个答案:

答案 0 :(得分:3)

您的代码中存在一些逻辑错误:

  1. 您需要以FileAccess.ReadWrite打开文件。
  2. FileMode.Open标志表示操作系统应该打开现有文件;但是,您随后将使用"Test"字符串覆盖其内容的前几个字节。这可能不是理想的行为。如果要替换文件,请使用FileMode.Truncate。如果要将字符串添加到文件末尾,请使用FileMode.Append
  3. 完成写入后,您需要刷新StreamWriter的内容。
  4. 您需要重置流的位置,以便BinaryReader阅读书面内容。
  5. 不要忘记处理你的溪流,读者,作家和(最好通过using块)。
  6. 示例代码:

    // Open the file.
    using (FileStream fs = new FileStream(fontFileNames, FileMode.Truncate, FileAccess.ReadWrite))
    using (StreamWriter sw = new StreamWriter(fs))
    { 
        //Add string to the file        
        sw.WriteLine("Test");
        sw.Flush();
        fs.Position = 0;
    
        // Create a BinaryReader on the file.
        using (BinaryReader br = new BinaryReader(fs))
        {
            /* rest of your code here */
        }
    }
    

    修改:附加的示例代码:

    // Open the file.
    using (FileStream fs = new FileStream(fontFileNames, FileMode.Append, FileAccess.Write))
    using (StreamWriter sw = new StreamWriter(fs))
    { 
        //Add string to the file        
        sw.WriteLine("Test");
    }
    
    // Create a BinaryReader on the file.
    using (FileStream fs = new FileStream(fontFileNames, FileMode.Open, FileAccess.Read))
    using (BinaryReader br = new BinaryReader(fs))
    {
        byte[] bytes = br.ReadBytes((int)fs.Length);
        /* rest of your code here */
    }
    

答案 1 :(得分:2)

出现此问题是因为您只使用写访问权限打开FileStream

FileStream fs = new FileStream(fontFileNames, FileMode.Open, FileAccess.Write);

然后尝试使用BinaryReader

进行阅读

bytes = br.ReadBytes(nLength);

而是打开具有读/写访问权限的流:

... = new FileStream(fontFileNames, FileMode.Open, FileAccess.ReadWrite);