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

来源:互联网 发布:windows 8.1怎么激活 编辑:程序博客网 时间:2024/06/12 20:49

文章参考: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());
  }}

0 0
原创粉丝点击