public void saveFileTags(string tags) //write text to ads for adding tags { string streamName = ":fileTags"; FileStream stream = NtfsAlternateStream.Open(this._path + streamName, FileAccess.ReadWrite, FileMode.OpenOrCreate, FileShare.None); stream.Close(); IEnumerable <NtfsAlternateStream> fileStream = NtfsAlternateStream.EnumerateStreams(this._path); foreach (NtfsAlternateStream ads in fileStream) { if (ads.StreamType.ToString().Equals("AlternateData")) { if (ads.Name.Equals(streamName + ":$DATA")) { string currentTags = getFileTag(); List <string> list = currentTags.Split(';').ToList(); if (!list.Contains(tags)) //check if tag exists already { NtfsAlternateStream.WriteAllText(this._path + streamName, currentTags + ";" + tags); } } } } }
private void Form1_DragDrop(object sender, DragEventArgs e) { string[] files = (string[])e.Data.GetData(DataFormats.FileDrop); foreach (string file in files) { if (file == null) { continue; } try { IEnumerable <NtfsAlternateStream> ntfsAlternateStreams = NtfsAlternateStream.EnumerateStreams(file); _path = file; listBox1.Items.Clear(); foreach (var stream in ntfsAlternateStreams) { listBox1.Items.Add(stream); } } catch (Exception ex) { Trace.WriteLine(ex); } } }
//delete all tags from selected files private void deleteTags(object sender, RoutedEventArgs e) { string docFilePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\Tags.xml"; XDocument xmlDocument = XDocument.Load(docFilePath); XDocument doc; doc = XDocument.Load(docFilePath); if (lb_tag.Items.Count > 0) { string messageBoxText = "Are you sure you want to delete tags from those files" + "?"; MessageBoxButton button = MessageBoxButton.YesNo; MessageBoxImage icon = MessageBoxImage.Warning; if (MessageBox.Show(messageBoxText, null, button, icon) == MessageBoxResult.No) { return; } List <string> fileslist = lb_tag.Items.Cast <String>().ToList(); string streamName = ":fileTags"; FileStream stream = NtfsAlternateStream.Open(fileslist[0] + streamName, FileAccess.ReadWrite, FileMode.OpenOrCreate, FileShare.None); stream.Close(); IEnumerable <NtfsAlternateStream> fileStream = NtfsAlternateStream.EnumerateStreams(fileslist[0]); for (var i = 0; i < fileslist.Count; i++) { NtfsAlternateStream.Delete(fileslist[i] + streamName); doc.Element("root").Elements("tag").Elements("path") .Where(x => x.Attribute("value").Value == fileslist[i]).Remove(); doc.Save(docFilePath); } MessageBox.Show("Tags deleted successfuly"); } else { MessageBox.Show(" you have to drag the files you want to tag "); } lb_tag.Items.Clear(); doc = XDocument.Load(docFilePath); return; }
private void Open() { if (listBox1.SelectedItem == null) { return; } var stream = listBox1.SelectedItem as NtfsAlternateStream; if (stream == null) { return; } OpenOrSave form = new OpenOrSave(); DialogResult dialogResult = form.ShowDialog(); if (dialogResult != DialogResult.OK && dialogResult != DialogResult.Yes) { return; } if (form.OpenSave == OpenSave.Open) { string text = NtfsAlternateStream.ReadAllText(_path + stream.Name); TextView textView = new TextView(text); textView.Show(); } else if (form.OpenSave == OpenSave.Save) { SaveFileDialog dialog = new SaveFileDialog(); dialog.FileName = ConvertUtilities.ToFileName(stream.Name); DialogResult showDialog = dialog.ShowDialog(); if (showDialog != DialogResult.OK && showDialog != DialogResult.Yes) { return; } using (var dstStream = dialog.OpenFile()) { using (var srcStream = NtfsAlternateStream.Open(_path + stream.Name, FileAccess.Read, FileMode.Open, FileShare.Read)) { srcStream.CopyTo(dstStream); } } } }
public void DeleteFileTags() //delete all ads from file { string streamName = ":fileTags"; if (checkTagFileExists(this._path)) { FileStream stream = NtfsAlternateStream.Open(this._path + streamName, FileAccess.ReadWrite, FileMode.OpenOrCreate, FileShare.None); stream.Close(); IEnumerable <NtfsAlternateStream> fileStream = NtfsAlternateStream.EnumerateStreams(this._path); foreach (NtfsAlternateStream ads in fileStream) { if (ads.StreamType.ToString().Equals("AlternateData")) { if (ads.Name.Equals(streamName + ":$DATA")) { NtfsAlternateStream.Delete(this._path + streamName); } } } } }
//get tags file content private string getFileStream(string fileName) //brings the stream the ads read the ads of the file { if (checkTagFileExists(fileName)) //if there is a tag { string streamName = ":fileTags"; FileStream stream = NtfsAlternateStream.Open(fileName + streamName, FileAccess.ReadWrite, FileMode.OpenOrCreate, FileShare.None); //open the ads stream.Close(); IEnumerable <NtfsAlternateStream> fileStream = NtfsAlternateStream.EnumerateStreams(fileName); //bring all the ads the file has whats in the stream foreach (NtfsAlternateStream ads in fileStream) //enter the filestream { if (ads.StreamType.ToString().Equals("AlternateData")) { if (ads.Name.Equals(streamName + ":$DATA")) //type of ads { return(Regex.Replace(NtfsAlternateStream.ReadAllText(fileName + streamName), "\n|\r", "")); } } } } return("The file doesn't have tags"); }
private void listBox1_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { Open(); } else if (e.KeyCode == Keys.Delete) { if (listBox1.SelectedItem == null) { return; } var stream = listBox1.SelectedItem as NtfsAlternateStream; if (stream == null) { return; } try { DialogResult dialogResult = MessageBox.Show(string.Format("Are you sure you want to delete '{0}'", stream.Name), "Confirm", MessageBoxButtons.YesNoCancel); if (dialogResult != DialogResult.OK && dialogResult != DialogResult.Yes) { return; } NtfsAlternateStream.Delete(_path + stream.Name); listBox1.Items.Remove(listBox1.SelectedItem); } catch (Exception ex) { Trace.WriteLine(ex); } } }
//Handle dropped files on listBox for tagging private void files_Drop(object sender, DragEventArgs e) { try { var files = (string[])e.Data.GetData(DataFormats.FileDrop); var myOtherFilesList = lb_tag.Items.Cast <String>().ToList(); if (files == null) //files are dragged from treeView { files = new[] { (string)((TreeViewItem)(e.Data.GetData(e.Data.GetFormats()[0]))).Tag }; } if ((files[0].ToString()).IndexOf('.') > -1) { string streamName = ":fileTags"; try { FileStream stream = NtfsAlternateStream.Open(files[0] + streamName, FileAccess.ReadWrite, FileMode.OpenOrCreate, FileShare.None); stream.Close(); } catch (UnauthorizedAccessException ex) { Console.WriteLine(ex.InnerException); MessageBox.Show("you have not authority to the file " + files[0]); return; } if (!myOtherFilesList.Contains(files[0])) { lb_tag.Items.Add(files[0]); return; } else { MessageBox.Show("The file you dragged already exsist in the dropped file list"); return; } } String[] allfiles = Directory.GetFiles(files[0], "*", SearchOption.AllDirectories); if (allfiles.Length == 0) { MessageBox.Show("The folder you want to drag has no files, drag ignored"); return; } for (var i = 0; i < allfiles.Length; i++) { if (!myOtherFilesList.Contains(allfiles[i])) { lb_tag.Items.Add(allfiles[i]); } else { MessageBox.Show("The files of the folder you dragged ,already exsist in the dropped file list"); return; } } } catch (UnauthorizedAccessException ex) { Console.WriteLine(ex.InnerException); MessageBox.Show("you have not authority to those files"); } }
private bool checkTagFileExists(string path) { string streamName = ":fileTags"; //ads needs file name : stream name return(NtfsAlternateStream.Exists(path + streamName)); }
// arrange the new remained tags after deleting tags from files public void saveFileTags1(string tags) //write text to ads for deleting tags { string streamName = ":fileTags"; string docFilePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\Tags.xml"; XDocument xmlDocument = XDocument.Load(docFilePath); XDocument doc; doc = XDocument.Load(docFilePath); string file1 = this._path; var ind_1 = 0; var ind1 = 0; string cat = tags; string subCat = ""; var ind = tags.IndexOf('.'); if (ind != -1) { ind_1 = ind - 1; ind1 = ind + 1; cat = tags.Substring(0, (ind)); // extract the category name from the tags string subCat = tags.Substring((ind1)); // extract the subCategory name from the tags string } string cc = getFileTag(); FileStream stream = NtfsAlternateStream.Open(this._path + streamName, FileAccess.ReadWrite, FileMode.OpenOrCreate, FileShare.None); stream.Close(); IEnumerable <NtfsAlternateStream> fileStream = NtfsAlternateStream.EnumerateStreams(this._path); // Enumerates the alternate streams from a file foreach (NtfsAlternateStream ads in fileStream) { if (ads.StreamType.ToString().Equals("AlternateData")) { if (ads.Name.Equals(streamName + ":$DATA")) { string currentTags = getFileTag(); // get the exsisting tag of file List <string> list = currentTags.Split(';').ToList(); // create a list of tags,each tag is sapereted by ; if (list.Contains(tags)) { var currentTagsLength = currentTags.Length; var tagsLength = tags.Length; for (var i = 1; i < currentTagsLength; i++) // check all the the tags the file has, { if (currentTags.Substring(i, tagsLength) == tags) // if tag already exsist in file will not be added { currentTags = currentTags.Remove(i - 1, tagsLength + 1); // remove the exsisting tag from tags string if (currentTags == "") // if the file hasn't any tag //We use the variable "currentTags" to order the tags of file (having type of String) //WriteAllText Methos of the class NtfsAlternateStream Creates a new file, //writes the specified string to the file, and then closes the file. If the target file already exists, it is overwritten. // This method supports NTFS alternate file streams paths. { NtfsAlternateStream.WriteAllText(this._path, currentTags); // NtfsAlternateStream Defines a utility class to read NTFS alternate streams data. } else { NtfsAlternateStream.WriteAllText(this._path + streamName, currentTags); //add the tags to the file } if (ind == -1) { doc.Element("root").Elements("tag").Elements("path").Where(x => x.Parent.Attribute("name").Value == cat && x.Attribute("value").Value == file1).Remove(); } else { doc.Element("root").Elements("tag").Elements("path").Where(x => x.Parent.Attribute("name").Value == cat && x.Parent.Attribute("value").Value == subCat && x.Attribute("value").Value == file1).Remove(); } doc.Save(docFilePath); return; } } } } } } }