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