C# Stream Copy .net流复制到另一个流

文章参考:http://www.jetwu.cn/archives/741




.net 4.5 中流复制的方法:Stream.CopyToAsync

1
2
3
public  Task CopyToAsync(
     Stream destination
)

示例:

1
2
3
4
5
6
7
8
9
10
11
12
string  StartDirectory =  @"c:\Users\exampleuser\start" ;            
string  EndDirectory =  @"c:\Users\exampleuser\end" ;            
foreach  ( string  filename  in  Directory.EnumerateFiles(StartDirectory))
             {               
              using  (FileStream SourceStream = File.Open(filename, FileMode.Open))
                {                  
                   using  (FileStream DestinationStream = File.Create(EndDirectory + filename.Substring(filename.LastIndexOf( '\\' ))))
                     {                     
                        await SourceStream.CopyToAsync(DestinationStream);
                     }
                 }
             }


.net 4.0 中流复制的方法:Stream.CopyTo

1
2
3
4
5
6
MemoryStream destination =  new  MemoryStream();
using  (FileStream source = File.Open( @"c:\temp\data.dat" ,FileMode.Open))
{
 
     source.CopyTo(destination);
}


.net 3.5及以下版本 流的复制 需要自己写代码实现

流复制方法一:

public static void CopyStream(Stream input, Stream output) {     
    byte[] buffer = new byte[32768];     
    long TempPos = input.Position;     
    while (true)         
    {         
        int read = input.Read (buffer, 0, buffer.Length);         
        if (read <= 0) break;         
        output.Write (buffer, 0, read);     
    }     
    input.Position = TempPos;// or you make Position = 0 to set it at the start }


流复制方法二:

1
2
3
4
5
6
public  static  void  CopyStream(Stream input, Stream output){
   using  (StreamReader reader =  new  StreamReader(input))
   using  (StreamWriter writer =  new  StreamWriter(output))
   {
     writer.Write(reader.ReadToEnd());
   }}


.net 4.5 中流复制的方法:Stream.CopyToAsync

1
2
3
public  Task CopyToAsync(
     Stream destination
)

示例:

1
2
3
4
5
6
7
8
9
10
11
12
string  StartDirectory =  @"c:\Users\exampleuser\start" ;            
string  EndDirectory =  @"c:\Users\exampleuser\end" ;            
foreach  ( string  filename  in  Directory.EnumerateFiles(StartDirectory))
             {               
              using  (FileStream SourceStream = File.Open(filename, FileMode.Open))
                {                  
                   using  (FileStream DestinationStream = File.Create(EndDirectory + filename.Substring(filename.LastIndexOf( '\\' ))))
                     {                     
                        await SourceStream.CopyToAsync(DestinationStream);
                     }
                 }
             }


.net 4.0 中流复制的方法:Stream.CopyTo

1
2
3
4
5
6
MemoryStream destination =  new  MemoryStream();
using  (FileStream source = File.Open( @"c:\temp\data.dat" ,FileMode.Open))
{
 
     source.CopyTo(destination);
}


.net 3.5及以下版本 流的复制 需要自己写代码实现

流复制方法一:

public static void CopyStream(Stream input, Stream output) {     
    byte[] buffer = new byte[32768];     
    long TempPos = input.Position;     
    while (true)         
    {         
        int read = input.Read (buffer, 0, buffer.Length);         
        if (read <= 0) break;         
        output.Write (buffer, 0, read);     
    }     
    input.Position = TempPos;// or you make Position = 0 to set it at the start }

流复制方法二:

1
2
3
4
5
6
public  static  void  CopyStream(Stream input, Stream output){
   using  (StreamReader reader =  new  StreamReader(input))
   using  (StreamWriter writer =  new  StreamWriter(output))
   {
     writer.Write(reader.ReadToEnd());
   }}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值