using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO.Compression; using System.IO; using System.Drawing; using System.Drawing.Imaging; using Trinet.Core.IO.Ntfs; namespace ConsoleApplication17 { class Program { static void Main(string[] args) { // 创建流 string path1 = @"C:\1\1.txt"; FileStream fs = File.Open(path1, FileMode.OpenOrCreate, FileAccess.Read, FileShare.None); string path2 = @"C:\1\2.txt"; FileInfo fileinfo = new FileInfo(path2); if(!File.Exists(path2)) { File.Create(path2);
}
AlternateDataStreamInfo adsi = fileinfo.GetAlternateDataStream("1.txt", System.IO.FileMode.OpenOrCreate ); byte[] b = new byte[fs.Length]; fs.Read(b, 0, b.Length); fs.Close(); string str = UTF8Encoding.Default.GetString(b);
StreamWriter sw = new StreamWriter(adsi.OpenWrite()); sw.WriteLine(str); sw.Close();
假如 有2个文件 1.txt 内容 11111 2.txt 内容(空)
type 1.txt>2.txt:1.txt notepad 2.txt 显示空
notepad 2.txt:1.txt 显示 11111
type 1.txt>>2.txt:2.txt --把文件1.txt写到另外一个空文件的文件流2.txt:2.txt中
del 1.txt --删除原本的文件1.txt
ren 2.txt 1.txt --把含有空文件内容的文件更名回去
notepad 1.txt:2.txt --打开文本的流 显示asdasd然而 本机的文件名称是1.txt
notepad 1.txt --看不到内容(内容进流里面了)
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Compression;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using Trinet.Core.IO.Ntfs;
namespace ConsoleApplication17
{
class Program
{
static void Main(string[] args)
{
// 创建流
string path1 = @"C:\1\1.txt";
FileStream fs = File.Open(path1, FileMode.OpenOrCreate, FileAccess.Read, FileShare.None);
string path2 = @"C:\1\2.txt";
FileInfo fileinfo = new FileInfo(path2);
if(!File.Exists(path2))
{
File.Create(path2);
}
AlternateDataStreamInfo adsi = fileinfo.GetAlternateDataStream("1.txt", System.IO.FileMode.OpenOrCreate );
byte[] b = new byte[fs.Length];
fs.Read(b, 0, b.Length);
fs.Close();
string str = UTF8Encoding.Default.GetString(b);
StreamWriter sw = new StreamWriter(adsi.OpenWrite());
sw.WriteLine(str);
sw.Close();
// 先删除文件1 然后把2改名为1
FileInfo filedel = new FileInfo(path1);
filedel.Delete();
File.Copy(path2, path1);
//把流的内容还原到原本的文件中 FileStream fst = adsi.OpenRead();
// 删除流的内容
//foreach (AlternateDataStreamInfo s in fileinfo.ListAlternateDataStreams())
//{
// s.Delete();//删除流
//}
} }
}
红的这句出错了 文件占用 但是 FS.CLOSE SW.CLOSR 拉
你的fileinfo还在占用着path2