废话不说,直接贴代码:
NTFSHelper.cs
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Threading;
- using Trinet.Core.IO.Ntfs;
- namespace NTFSHelper
- {
- struct TStream
- {
- public string FileName;
- public string StreamName;
- public long StreamSize;
- public string View;
- }
- class CState
- {
- public DateTime BeginTime { set; get; }
- public string CurrentFile { set; get; }
- public int Count { set; get; }
- public bool Finish { set; get; }
- public bool isPause { set; get; }
- public CState()
- {
- Reset();
- }
- public void Reset()
- {
- this.BeginTime = DateTime.Now;
- this.Count = 0;
- this.CurrentFile = string.Empty;
- this.Finish = true;
- this.isPause = false;
- }
- }
- class NTFSHelper
- {
- public CState State { set; get; }
- List<string> PathList = null;
- List<TStream> BackResult;
- Thread Thd = null;
- public NTFSHelper(List<string> PathList, CState State, List<TStream> BackResult)
- {
- this.State = State;
- this.PathList = PathList;
- this.BackResult = BackResult;
- }
- public void BeginProcess()
- {
- State.Reset();
- State.Finish = false;
- Thd = new Thread(new ThreadStart(DoProcess));
- Thd.Start();
- }
- public void EndProcess()
- {
- if(Thd == null)
- return;
- while(Thd.ThreadState == ThreadState.SuspendRequested)
- ;
- if(Thd.ThreadState == ThreadState.Suspended)
- Thd.Resume();
- Thd.Abort();
- State.Finish = true;
- }
- public void SuspendProcess()
- {
- if(Thd != null && Thd.ThreadState == ThreadState.Running)
- Thd.Suspend();
- }
- public void ResumeProcess()
- {
- if(Thd != null && Thd.ThreadState == ThreadState.Suspended)
- Thd.Resume();
- }
- private void DoProcess()
- {
- State.Reset();
- State.Finish = false;
- foreach(string Path in PathList)
- FindPath(Path);
- State.Finish = true;
- }
- private void FindPath(string Path)
- {
- try
- {
- DirectoryInfo PathInfo = new DirectoryInfo(Path);
- FileInfo[] files = PathInfo.GetFiles();
- DirectoryInfo[] dirs = PathInfo.GetDirectories();
- foreach(FileInfo Finfo in files)
- {
- State.CurrentFile = Finfo.FullName;
- FindStreams(Finfo.ListAlternateDataStreams());
- }
- foreach(DirectoryInfo Dinfo in dirs)
- {
- State.CurrentFile = Dinfo.FullName;
- FindStreams(Dinfo.ListAlternateDataStreams());
- }
- foreach(DirectoryInfo D in dirs)
- FindPath(D.FullName);
- }
- catch { }
- }
- private void FindStreams(IList<AlternateDataStreamInfo> Streams)
- {
- foreach(var stream in Streams)
- {
- TStream T = new TStream();
- T.FileName = stream.FilePath;
- T.StreamName = stream.Name;
- T.StreamSize = stream.Size;
- string str = stream.OpenText().ReadToEnd();
- T.View = str.Substring(0, Math.Min(str.Length, 30));
- lock(BackResult)
- BackResult.Add(T);
- }
- }
- public static void DeleteStream(string Path, string Name)
- {
- //不知道文件夹 有没有ntfs流
- FileInfo Finfo = new FileInfo(Path);
- Finfo.DeleteAlternateDataStream(Name);
- }
- public static Stream GetStream(string Path, string Name)
- {
- FileInfo Finfo = new FileInfo(Path);
- return Finfo.GetAlternateDataStream(Name).Open(FileMode.Open);
- }
- }
- }
Form1.cs
- using System;
- using System.Collections.Generic;
- using System.Threading;
- using System.Windows.Forms;
- namespace NTFSHelper
- {
- public partial class Form1 : Form
- {
- CState State = new CState();
- List<string> PathList = null;
- List<TStream> BackResult = null;
- Form2 ViewForm = null;
- NTFSHelper Man;
- public Form1()
- {
- InitializeComponent();
- PathList = new List<string>();
- BackResult = new List<TStream>();
- Man = new NTFSHelper(PathList, State, BackResult);
- }
- private void button1_Click(object sender, EventArgs e)
- {
- FolderBrowserDialog fbd = new FolderBrowserDialog();
- ListView.ListViewItemCollection lvic = lv_path.Items;
- fbd.ShowNewFolderButton = false;
- string path;
- bool exists;
- DialogResult dr = fbd.ShowDialog();
- if(dr == DialogResult.OK)
- {
- path = fbd.SelectedPath;
- exists = false;
- foreach(ListViewItem s in lvic)
- if(path.ToLower().IndexOf(s.Text.ToLower()) == 0)
- {
- exists = true;
- break;
- }
- if(exists)
- {
- MessageBox.Show(string.Format("路径 /"{0}/" 已经存在", path), "路径已存在", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- exists = false;
- foreach(ListViewItem s in lvic)
- if(s.Text.ToLower().IndexOf(path.ToLower()) == 0)
- {
- exists = true;
- break;
- }
- if(exists)
- {
- dr = MessageBox.Show(string.Format("路径 /"{0}/" 是已选定目录的父级,是否进行替换?", path), "是否替换子路径", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
- if(dr == DialogResult.No)
- return;
- }
- foreach(ListViewItem s in lvic)
- if(s.Text.ToLower().IndexOf(path.ToLower()) == 0)
- s.Remove();
- ListViewItem lvi = new ListViewItem();
- lvi.Text = path;
- lv_path.Items.Add(lvi);
- }
- }
- private void button2_Click(object sender, EventArgs e)
- {
- foreach(ListViewItem item in lv_path.SelectedItems)
- item.Remove();
- }
- private void button3_Click(object sender, EventArgs e)
- {
- lv_path.Items.Clear();
- }
- private void button4_Click(object sender, EventArgs e)
- {
- if(lv_path.Items.Count == 0)
- {
- MessageBox.Show("请至少选择一个路径", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- PathList.Clear();
- foreach(ListViewItem item in lv_path.Items)
- PathList.Add(item.Text);
- Man.BeginProcess();
- lv_result.Items.Clear();
- button1.Enabled = false;
- button2.Enabled = false;
- button3.Enabled = false;
- button4.Enabled = false;
- button5.Enabled = true;
- button6.Enabled = true;
- timer1.Enabled = true;
- }
- private void timer1_Tick(object sender, EventArgs e)
- {
- label2.Text = State.CurrentFile;
- lock(BackResult)
- {
- foreach(TStream item in BackResult)
- {
- ListViewItem lvi = new ListViewItem(new string[] { State.Count++.ToString(), item.FileName, item.StreamName, item.StreamSize.ToString(), item.View });
- lv_result.Items.Add(lvi);
- }
- BackResult.Clear();
- }
- if(State.Finish && BackResult.Count == 0)
- {
- button1.Enabled = true;
- button2.Enabled = true;
- button3.Enabled = true;
- button4.Enabled = true;
- button5.Enabled = false;
- button6.Enabled = false;
- button6.Text = "暂停";
- label2.Text = "-";
- timer1.Enabled = false;
- }
- }
- private void button5_Click(object sender, EventArgs e)
- {
- Man.EndProcess();
- }
- private void button6_Click(object sender, EventArgs e)
- {
- if(State.isPause)
- {
- Man.ResumeProcess();
- button6.Text = "暂停";
- State.isPause = false;
- }
- else
- {
- Man.SuspendProcess();
- button6.Text = "恢复";
- State.isPause = true;
- }
- }
- private void button7_Click(object sender, EventArgs e)
- {
- if(lv_result.SelectedItems.Count > 5)
- if(DialogResult.No == MessageBox.Show("一次要显示显示的内容过多,是否要继续显示?", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
- return;
- foreach(ListViewItem item in lv_result.SelectedItems)
- {
- ViewForm = new Form2(item.SubItems[1].Text, item.SubItems[2].Text);
- new Thread(new ThreadStart(DoViewForm)).Start();
- while(ViewForm != null)
- Thread.Sleep(100);
- }
- }
- private void DoViewForm()
- {
- Thread.CurrentThread.IsBackground = true;
- Form2 f = ViewForm;
- ViewForm = null;
- Application.Run(f);
- }
- private void button8_Click(object sender, EventArgs e)
- {
- try
- {
- foreach(ListViewItem item in lv_result.SelectedItems)
- {
- NTFSHelper.DeleteStream(item.SubItems[1].Text, item.SubItems[2].Text);
- item.Remove();
- }
- }
- catch
- {
- MessageBox.Show("删除流 /"{0}:{1}/" 的时候出错。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- }
- }
完整代码下载地址: http://download.youkuaiyun.com/source/732222