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; }
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"); }
// 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; } } } } } } }