将附件内容流写入光盘上的文件时跳过前导字节 [英] Skip Leading Bytes When Writing Attachment Content Stream To File On The Disc

查看:99
本文介绍了将附件内容流写入光盘上的文件时跳过前导字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我在将附件内容流保存到文件时遇到问题,问题是保存的文件包含前导字节,特别是在我的情况下有三个问号??? 。

问题似乎很简单,但我不确定它背后是什么,是写入文件的BOM(字节顺序标记)还是流的前缀长度?

我尝试了几种方法,但是所有方法都有相同的结果:

这是我用过的方法:

第一个:

  public   void  saveAttachment(System.Net.Mail.Attachment attachment)
{
StreamReader reader = new StreamReader(attachment.ContentStream);
File.WriteAllText(attachment.Name,reader.ReadToEnd());
}



第二个(类似于第一个):

  public   void  saveAttachment(System.Net.Mail.Attachment attachment)
{
StreamWriter writer = new StreamWriter(attachment.Name);
StreamReader reader = new StreamReader(attachment.ContentStream);
writer.Write(reader.ReadToEnd());
writer.Close();
}





基于 link





  public   void  saveMailAttachment(System.Net.Mail.Attachment attachment)
{
byte [] allBytes = new byte [attachment.ContentStream.Length];

int bytesRead = attachment.ContentStream.Read(allBytes, 0 ,( int )attachment.ContentStream.Length);

string destinationFile = attachment.Name;

BinaryWriter writer = new BinaryWriter( new FileStream(destinationFile,FileMode.OpenOrCreate ,FileAccess.Write,FileShare.None));
writer.Write(allBytes);
writer.Close();
}







我试过玩字节数组,但我不认为这是解决问题的一种非常可靠的方法,非常感谢任何帮助。

提前谢谢。

解决方案

如果你总是有3,正好3个文件开头的标记字符,根据你的第二个实现,有一个非常简单的方法来实现它:



  public   void  saveAttachment(System.Net.Mail.Attachment attachment)
{
使用 var writer = new StreamWriter(attachment.Name))
{
使用 var reader = new StreamReader(attachment.ContentStream))
{
// 设置流指针
reader.BaseStream.Seek( 3 ,SeekOrigin.Begin);
writer.Write(reader.Read());
writer.Close();
}
}
}





免费配置逻辑。 :)


Hi all,
I'm facing a problem with saving attachment content stream to a file, the problem is that the saved files contains leading bytes, specifically in my case three question marks ??? .
The problem seems simple but i'm not sure what is behind it , is it BOM (Byte Order Mark) written to the file or is it prefix length of the stream ?
I've tried several ways , but with all had the same results:
Here is my used methods:
First one:

public void saveAttachment(System.Net.Mail.Attachment attachment)
        {
            StreamReader reader = new StreamReader(attachment.ContentStream);
            File.WriteAllText(attachment.Name, reader.ReadToEnd());
        }


Second one (similar to the first):

public void saveAttachment(System.Net.Mail.Attachment attachment)
        {
            StreamWriter writer = new StreamWriter(attachment.Name);
            StreamReader reader = new StreamReader(attachment.ContentStream);
            writer.Write(reader.ReadToEnd());
            writer.Close();
        }



Third method based on the link :


public void saveMailAttachment(System.Net.Mail.Attachment attachment)
{
    byte[] allBytes = new byte[attachment.ContentStream.Length];

    int bytesRead = attachment.ContentStream.Read(allBytes, 0, (int)attachment.ContentStream.Length);

    string destinationFile = attachment.Name;

    BinaryWriter writer = new BinaryWriter(new FileStream(destinationFile, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None));
    writer.Write(allBytes);
    writer.Close();
}




I tried playing with the bytes array , but i don't think it's a very reliable way to solve the problem , any help would be very appreciated.
Thanks in advance.

解决方案

If you always have 3, and exactly 3, marker characters at the start of the file, there's a very easy way to implement this based on your second implementation:

public void saveAttachment(System.Net.Mail.Attachment attachment)
{
    using(var writer = new StreamWriter(attachment.Name))
    {
        using(var reader = new StreamReader(attachment.ContentStream))
        {
            // Set the stream pointer
            reader.BaseStream.Seek(3, SeekOrigin.Begin);
            writer.Write(reader.Read());
            writer.Close();
        }
    }
}



Dispose logic included free of charge. :)


这篇关于将附件内容流写入光盘上的文件时跳过前导字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆