操作NTFS数据流


废话不说,直接贴代码:

NTFSHelper.cs

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Threading;
  5. using Trinet.Core.IO.Ntfs;
  6. namespace NTFSHelper
  7. {
  8.     struct TStream
  9.     {
  10.         public string FileName;
  11.         public string StreamName;
  12.         public long StreamSize;
  13.         public string View;
  14.     }
  15.     class CState
  16.     {
  17.         public DateTime BeginTime { setget; }
  18.         public string CurrentFile { setget; }
  19.         public int Count { setget; }
  20.         public bool Finish { setget; }
  21.         public bool isPause { setget; }
  22.         public CState()
  23.         {
  24.             Reset();
  25.         }
  26.         public void Reset()
  27.         {
  28.             this.BeginTime = DateTime.Now;
  29.             this.Count = 0;
  30.             this.CurrentFile = string.Empty;
  31.             this.Finish = true;
  32.             this.isPause = false;
  33.         }
  34.     }
  35.     class NTFSHelper
  36.     {
  37.         public CState State { setget; }
  38.         List<string> PathList = null;
  39.         List<TStream> BackResult;
  40.         Thread Thd = null;
  41.         public NTFSHelper(List<string> PathList, CState State, List<TStream> BackResult)
  42.         {
  43.             this.State = State;
  44.             this.PathList = PathList;
  45.             this.BackResult = BackResult;
  46.         }
  47.         public void BeginProcess()
  48.         {
  49.             State.Reset();
  50.             State.Finish = false;
  51.             Thd = new Thread(new ThreadStart(DoProcess));
  52.             Thd.Start();
  53.         }
  54.         public void EndProcess()
  55.         {
  56.             if(Thd == null)
  57.                 return;
  58.             while(Thd.ThreadState == ThreadState.SuspendRequested)
  59.                 ;
  60.             if(Thd.ThreadState == ThreadState.Suspended)
  61.                 Thd.Resume();
  62.             Thd.Abort();
  63.             State.Finish = true;
  64.         }
  65.         public void SuspendProcess()
  66.         {
  67.             if(Thd != null && Thd.ThreadState == ThreadState.Running)
  68.                 Thd.Suspend();
  69.         }
  70.         public void ResumeProcess()
  71.         {
  72.             if(Thd != null && Thd.ThreadState == ThreadState.Suspended)
  73.                 Thd.Resume();
  74.         }
  75.         private void DoProcess()
  76.         {
  77.             State.Reset();
  78.             State.Finish = false;
  79.             foreach(string Path in PathList)
  80.                 FindPath(Path);
  81.             State.Finish = true;
  82.         }
  83.         private void FindPath(string Path)
  84.         {
  85.             try
  86.             {
  87.                 DirectoryInfo PathInfo = new DirectoryInfo(Path);
  88.                 FileInfo[] files = PathInfo.GetFiles();
  89.                 DirectoryInfo[] dirs = PathInfo.GetDirectories();
  90.                 foreach(FileInfo Finfo in files)
  91.                 {
  92.                     State.CurrentFile = Finfo.FullName;
  93.                     FindStreams(Finfo.ListAlternateDataStreams());
  94.                 }
  95.                 foreach(DirectoryInfo Dinfo in dirs)
  96.                 {
  97.                     State.CurrentFile = Dinfo.FullName;
  98.                     FindStreams(Dinfo.ListAlternateDataStreams());
  99.                 }
  100.                 foreach(DirectoryInfo D in dirs)
  101.                     FindPath(D.FullName);
  102.             }
  103.             catch { }
  104.         }
  105.         private void FindStreams(IList<AlternateDataStreamInfo> Streams)
  106.         {
  107.             foreach(var stream in Streams)
  108.             {
  109.                 TStream T = new TStream();
  110.                 T.FileName = stream.FilePath;
  111.                 T.StreamName = stream.Name;
  112.                 T.StreamSize = stream.Size;
  113.                 string str = stream.OpenText().ReadToEnd();
  114.                 T.View = str.Substring(0, Math.Min(str.Length, 30));
  115.                 lock(BackResult)
  116.                     BackResult.Add(T);
  117.             }
  118.         }
  119.         public static void DeleteStream(string Path, string Name)
  120.         {
  121.             //不知道文件夹 有没有ntfs流
  122.             FileInfo Finfo = new FileInfo(Path);
  123.             Finfo.DeleteAlternateDataStream(Name);
  124.         }
  125.         public static Stream GetStream(string Path, string Name)
  126.         {
  127.             FileInfo Finfo = new FileInfo(Path);
  128.             return Finfo.GetAlternateDataStream(Name).Open(FileMode.Open);
  129.         }
  130.     }
  131. }

Form1.cs
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. using System.Windows.Forms;
  5. namespace NTFSHelper
  6. {
  7.     public partial class Form1 : Form
  8.     {
  9.         CState State = new CState();
  10.         List<string> PathList = null;
  11.         List<TStream> BackResult = null;
  12.         Form2 ViewForm = null;
  13.         NTFSHelper Man;
  14.         public Form1()
  15.         {
  16.             InitializeComponent();
  17.             PathList = new List<string>();
  18.             BackResult = new List<TStream>();
  19.             Man = new NTFSHelper(PathList, State, BackResult);
  20.         }
  21.         private void button1_Click(object sender, EventArgs e)
  22.         {
  23.             FolderBrowserDialog fbd = new FolderBrowserDialog();
  24.             ListView.ListViewItemCollection lvic = lv_path.Items;
  25.             fbd.ShowNewFolderButton = false;
  26.             string path;
  27.             bool exists;
  28.             DialogResult dr = fbd.ShowDialog();
  29.             if(dr == DialogResult.OK)
  30.             {
  31.                 path = fbd.SelectedPath;
  32.                 exists = false;
  33.                 foreach(ListViewItem s in lvic)
  34.                     if(path.ToLower().IndexOf(s.Text.ToLower()) == 0)
  35.                     {
  36.                         exists = true;
  37.                         break;
  38.                     }
  39.                 if(exists)
  40.                 {
  41.                     MessageBox.Show(string.Format("路径 /"{0}/" 已经存在", path), "路径已存在", MessageBoxButtons.OK, MessageBoxIcon.Error);
  42.                     return;
  43.                 }
  44.                 exists = false;
  45.                 foreach(ListViewItem s in lvic)
  46.                     if(s.Text.ToLower().IndexOf(path.ToLower()) == 0)
  47.                     {
  48.                         exists = true;
  49.                         break;
  50.                     }
  51.                 if(exists)
  52.                 {
  53.                     dr = MessageBox.Show(string.Format("路径 /"{0}/" 是已选定目录的父级,是否进行替换?", path), "是否替换子路径", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
  54.                     if(dr == DialogResult.No)
  55.                         return;
  56.                 }
  57.                 foreach(ListViewItem s in lvic)
  58.                     if(s.Text.ToLower().IndexOf(path.ToLower()) == 0)
  59.                         s.Remove();
  60.                 ListViewItem lvi = new ListViewItem();
  61.                 lvi.Text = path;
  62.                 lv_path.Items.Add(lvi);
  63.             }
  64.         }
  65.         private void button2_Click(object sender, EventArgs e)
  66.         {
  67.             foreach(ListViewItem item in lv_path.SelectedItems)
  68.                 item.Remove();
  69.         }
  70.         private void button3_Click(object sender, EventArgs e)
  71.         {
  72.             lv_path.Items.Clear();
  73.         }
  74.         private void button4_Click(object sender, EventArgs e)
  75.         {
  76.             if(lv_path.Items.Count == 0)
  77.             {
  78.                 MessageBox.Show("请至少选择一个路径""错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  79.                 return;
  80.             }
  81.             PathList.Clear();
  82.             foreach(ListViewItem item in lv_path.Items)
  83.                 PathList.Add(item.Text);
  84.             Man.BeginProcess();
  85.             lv_result.Items.Clear();
  86.             button1.Enabled = false;
  87.             button2.Enabled = false;
  88.             button3.Enabled = false;
  89.             button4.Enabled = false;
  90.             button5.Enabled = true;
  91.             button6.Enabled = true;
  92.             timer1.Enabled = true;
  93.         }
  94.         private void timer1_Tick(object sender, EventArgs e)
  95.         {
  96.             label2.Text = State.CurrentFile;
  97.             lock(BackResult)
  98.             {
  99.                 foreach(TStream item in BackResult)
  100.                 {
  101.                     ListViewItem lvi = new ListViewItem(new string[] { State.Count++.ToString(), item.FileName, item.StreamName, item.StreamSize.ToString(), item.View });
  102.                     lv_result.Items.Add(lvi);
  103.                 }
  104.                 BackResult.Clear();
  105.             }
  106.             if(State.Finish && BackResult.Count == 0)
  107.             {
  108.                 button1.Enabled = true;
  109.                 button2.Enabled = true;
  110.                 button3.Enabled = true;
  111.                 button4.Enabled = true;
  112.                 button5.Enabled = false;
  113.                 button6.Enabled = false;
  114.                 button6.Text = "暂停";
  115.                 label2.Text = "-";
  116.                 timer1.Enabled = false;
  117.             }
  118.         }
  119.         private void button5_Click(object sender, EventArgs e)
  120.         {
  121.             Man.EndProcess();
  122.         }
  123.         private void button6_Click(object sender, EventArgs e)
  124.         {
  125.             if(State.isPause)
  126.             {
  127.                 Man.ResumeProcess();
  128.                 button6.Text = "暂停";
  129.                 State.isPause = false;
  130.             }
  131.             else
  132.             {
  133.                 Man.SuspendProcess();
  134.                 button6.Text = "恢复";
  135.                 State.isPause = true;
  136.             }
  137.         }
  138.         private void button7_Click(object sender, EventArgs e)
  139.         {
  140.             if(lv_result.SelectedItems.Count > 5)
  141.                 if(DialogResult.No == MessageBox.Show("一次要显示显示的内容过多,是否要继续显示?""确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
  142.                     return;
  143.             foreach(ListViewItem item in lv_result.SelectedItems)
  144.             {
  145.                 ViewForm = new Form2(item.SubItems[1].Text, item.SubItems[2].Text);
  146.                 new Thread(new ThreadStart(DoViewForm)).Start();
  147.                 while(ViewForm != null)
  148.                     Thread.Sleep(100);
  149.             }
  150.         }
  151.         private void DoViewForm()
  152.         {
  153.             Thread.CurrentThread.IsBackground = true;
  154.             Form2 f = ViewForm;
  155.             ViewForm = null;
  156.             Application.Run(f);
  157.         }
  158.         private void button8_Click(object sender, EventArgs e)
  159.         {
  160.             try
  161.             {
  162.                 foreach(ListViewItem item in lv_result.SelectedItems)
  163.                 {
  164.                     NTFSHelper.DeleteStream(item.SubItems[1].Text, item.SubItems[2].Text);
  165.                     item.Remove();
  166.                 }
  167.             }
  168.             catch
  169.             {
  170.                 MessageBox.Show("删除流 /"{0}:{1}/" 的时候出错。""错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  171.             }
  172.         }
  173.     }
  174. }


完整代码下载地址: http://download.youkuaiyun.com/source/732222
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值