예제 #1
0
 public void Close()
 {
     if (propertyWriter != null)
     {
         propertyWriter.Close();
     }
 }
예제 #2
0
        public static void editFileDates(MediaProcessingContext context)
        {
            ShellFile extendedFile = ShellFile.FromFilePath(context.movedFile.FullName);

            File.SetCreationTime(context.movedFile.FullName, context.oldestFileDate);
            File.SetLastWriteTime(context.movedFile.FullName, context.oldestFileDate);
            File.SetLastAccessTime(context.movedFile.FullName, context.oldestFileDate);

            ShellPropertyWriter writer = null;

            try {
                writer = extendedFile.Properties.GetPropertyWriter();
                if (context.mimeType.StartsWith("image"))
                {
                    writer.WriteProperty(SystemProperties.System.Photo.DateTaken, context.oldestFileDate);
                }
                if (context.mimeType.StartsWith("video"))
                {
                    writer.WriteProperty(SystemProperties.System.Media.DateEncoded, context.oldestFileDate);
                }
            } catch (Exception ignored) {
                Console.WriteLine("ERROR : {0}`s metadata writing failed", context.movedFile.Name);
            } finally {
                if (writer != null)
                {
                    writer.Close();
                }
            }
        }
예제 #3
0
        /// <summary>
        /// Sets the title attribute in the given file.
        /// </summary>
        /// <param name="file">Target file</param>
        /// <param name="title">The song's title</param>
        public static void AddTitle(string file, string title)
        {
            var _file = ShellFile.FromFilePath(file);

            try {
                ShellPropertyWriter propertyWriter = _file.Properties.GetPropertyWriter();
                propertyWriter.WriteProperty(SystemProperties.System.Title, new string[] { title });
                propertyWriter.Close();
            } catch (Exception e) {
            }
        }
예제 #4
0
        /// <summary>
        /// Sets the artist attribute in the given file.
        /// </summary>
        /// <param name="file">Target file</param>
        /// <param name="artist">The song's artist</param>
        public static void AddArtist(string file, string artist)
        {
            var _file = ShellFile.FromFilePath(file);

            try {
                ShellPropertyWriter propertyWriter = _file.Properties.GetPropertyWriter();
                propertyWriter.WriteProperty(SystemProperties.System.Music.Artist, new string[] { artist });
                propertyWriter.Close();
            } catch (Exception e) {
            }
        }
예제 #5
0
        private void ChangeTitleToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                string[] files = Directory.GetFiles(path, "*.mp3", SearchOption.AllDirectories);

                for (int i = 0; i < files.Length; i++)
                {
                    FileInfo file = new FileInfo(files[i]);
                    if (NameCheck(file.Name) == "")
                    {
                        var fileshell = ShellFile.FromFilePath(file.DirectoryName + "\\" + file.Name);

                        Shell32.Shell      shell       = new Shell32.Shell();
                        var                strFileName = file.DirectoryName + "\\" + file.Name;
                        Shell32.Folder     objFolder   = shell.NameSpace(System.IO.Path.GetDirectoryName(strFileName));
                        Shell32.FolderItem folderItem  = objFolder.ParseName(System.IO.Path.GetFileName(strFileName));
                        var                title       = objFolder.GetDetailsOf(folderItem, 21);

                        if (title == "" || title.Contains("ntitled") || title.Contains("nknown") || title.Contains("nknown") || title.Contains("http") || title.Contains("www.") || title.Contains(".com") || title.Contains("Track"))
                        {
                            string   name    = objFolder.GetDetailsOf(folderItem, 0);
                            string[] newName = Regex.Split(name, "-");

                            if (newName.Length >= 2 && newName[1] != null)
                            {
                                string[] newest = Regex.Split(newName[1], "\\.");

                                ShellPropertyWriter propertyWriter = fileshell.Properties.GetPropertyWriter();
                                try
                                {
                                    propertyWriter.WriteProperty(SystemProperties.System.Title, new string[] { newest[0] });
                                }
                                finally
                                {
                                    propertyWriter.Close();
                                }
                            }
                        }
                    }
                    progressBar1.Value = Convert.ToInt32(100 * (i + 1) / files.Length);

                    if (i == files.Length - 1)
                    {
                        progressBar1.Value = 100;
                    }
                }
            }
            catch (FileNotFoundException)
            {
                MessageBox.Show("Files not found exception!", "Error!");
            }
        }
예제 #6
0
        private static void updateMetadata(string fileNameWithoutExtension, string fileExt)
        {
            ShellFile           file   = ShellFile.FromFilePath(fileNameWithoutExtension + fileExt);
            ShellPropertyWriter writer = file.Properties.GetPropertyWriter();

            writer.WriteProperty(SystemProperties.System.Title, "test");
            writer.Close();
            //Debug.WriteLine(file.Properties.System.Title.Value);
            //file.Properties.System.Title.Value = "";// fileNameWithoutExtension;
            //FileInfo info = new FileInfo(fileName);
            //FileAttributes attributes = info.Attributes;
            //Debug.WriteLine(attributes.ToString());
        }
예제 #7
0
        private static void UpdateExif(string filePath, string title, string keywords)
        {
            var file = ShellFile.FromFilePath(filePath);

            using (ShellPropertyWriter propertyWriter = file.Properties.GetPropertyWriter())
            {
                propertyWriter.WriteProperty(SystemProperties.System.Title, title);
                propertyWriter.WriteProperty(SystemProperties.System.Subject, title);
                propertyWriter.WriteProperty(SystemProperties.System.Keywords, keywords);
                propertyWriter.WriteProperty(SystemProperties.System.Comment, keywords);

                propertyWriter.Close();
            }
        }
예제 #8
0
        public void FilePropertiesTest()
        {
            var file = ShellFile.FromFilePath(@"C:\Temp\test.jpg");

            //拡張プロパティ取得
            Console.WriteLine(file.Properties.System.Title.Value);
            Console.WriteLine(file.Properties.System.Author.Value);
            Console.WriteLine(file.Properties.System.Comment.Value);

            //拡張プロパティセット
            ShellPropertyWriter propertyWriter = file.Properties.GetPropertyWriter();

            propertyWriter.WriteProperty(SystemProperties.System.Title, new string[] { "タイトル" });
            propertyWriter.WriteProperty(SystemProperties.System.Author, new string[] { "著者" });
            propertyWriter.WriteProperty(SystemProperties.System.Comment, new string[] { "コメント" });
            propertyWriter.Close();
        }
예제 #9
0
        void doIt()
        {
            DirectoryInfo d = new DirectoryInfo(folderpath);

            foreach (var file in d.GetFiles("*.jpg"))
            {
                string filePath = file.FullName;
                string name     = Path.GetFileNameWithoutExtension(filePath);
                var    img      = ShellFile.FromFilePath(filePath);
                try
                {
                    ShellPropertyWriter propertyWriter = img.Properties.GetPropertyWriter();
                    propertyWriter.WriteProperty(SystemProperties.System.Title, name);
                    propertyWriter.WriteProperty(SystemProperties.System.Subject, name);
                    propertyWriter.WriteProperty(SystemProperties.System.Rating, 99);
                    propertyWriter.WriteProperty(SystemProperties.System.Comment, "I like " + name);
                    propertyWriter.WriteProperty(SystemProperties.System.Title, name);
                    propertyWriter.WriteProperty(SystemProperties.System.Author, new string[] { comboBox1.Text });
                    propertyWriter.WriteProperty(SystemProperties.System.Copyright, comboBox1.Text);
                    propertyWriter.Close();
                    // Read and Write:

                    /*
                     * img.Properties.System.Title.Value = file.Name;
                     * img.Properties.System.Subject.Value = file.Name;
                     * img.Properties.System.Rating.Value = 99; // 5 stars
                     * img.Properties.System.Comment.Value = "I like " + file.Name;
                     * img.Properties.System.Author.Value = new string[] { comboBox1.Text };
                     * img.Properties.System.Copyright.Value = comboBox1.Text; */
                }
                catch (Exception e)
                {
                    //MessageBox.Show("Error !!!");
                    continue;
                }
            }
        }
예제 #10
0
        private void LyricsUpdateToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                string[] files = Directory.GetFiles(path, "*.mp3", SearchOption.AllDirectories);
                errorList = "No lyrics for the following: ";

                for (int i = 0; i < files.Length; i++)
                {
                    FileInfo file        = new FileInfo(files[i]);
                    var      strFileName = file.DirectoryName + "\\" + file.Name;

                    ShellObject song   = ShellObject.FromParsingName(strFileName);
                    string      artist = GetValue(song.Properties.GetProperty(SystemProperties.System.Music.AlbumArtist));
                    string      title  = GetValue(song.Properties.GetProperty(SystemProperties.System.Title));
                    string      lyrics = GetValue(song.Properties.GetProperty(SystemProperties.System.Music.Lyrics));

                    if (artist != "" && title != "" && (lyrics == "" || lyrics.Contains(".com") || lyrics.Contains("www.") || lyrics.Contains("http")))
                    {
                        //string lyrics = GetValue(song.Properties.GetProperty(SystemProperties.System.Music.Lyrics));

                        var fileshell = ShellFile.FromFilePath(strFileName);
                        ShellPropertyWriter propertyWriter = fileshell.Properties.GetPropertyWriter();

                        try
                        {
                            var lyricsValues = SingleEdit.ChartLyricsGet(artist, title);

                            if (lyricsValues.Item1 != null && lyricsValues.Item1 != "")
                            {
                                propertyWriter.WriteProperty(SystemProperties.System.Music.Lyrics, new string[] { lyricsValues.Item1 });
                            }
                            else if (lyricsValues.Item2 == true)
                            {
                                break;
                            }
                            else
                            {
                                errorList += Environment.NewLine + file.Name;
                            }
                        }
                        finally
                        {
                            propertyWriter.Close();
                        }
                    }
                    progressBar1.Value = Convert.ToInt32(100 * (i + 1) / files.Length);

                    if (i == files.Length - 1)
                    {
                        progressBar1.Value = 100;
                    }
                }
                ErrorList errorForm = new ErrorList(errorList);
                errorForm.ShowDialog();
            }
            catch (FileNotFoundException)
            {
                System.Windows.Forms.MessageBox.Show("Files not found exception!", "Error!");
            }
        }
예제 #11
0
        private void MusixmatchUpdateToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                string[] files = Directory.GetFiles(path, "*.mp3", SearchOption.AllDirectories);
                errorList = "No data for the following: ";

                for (int i = 0; i < files.Length; i++)
                {
                    FileInfo file        = new FileInfo(files[i]);
                    var      strFileName = file.DirectoryName + "\\" + file.Name;

                    ShellObject song   = ShellObject.FromParsingName(strFileName);
                    string      artist = GetValue(song.Properties.GetProperty(SystemProperties.System.Music.AlbumArtist));
                    string      title  = GetValue(song.Properties.GetProperty(SystemProperties.System.Title));

                    if (artist != "" && title != "")
                    {
                        var    year  = GetValue(song.Properties.GetProperty(SystemProperties.System.Media.Year));
                        string album = GetValue(song.Properties.GetProperty(SystemProperties.System.Music.AlbumTitle));

                        Shell32.Shell      shell      = new Shell32.Shell();
                        Shell32.Folder     objFolder  = shell.NameSpace(System.IO.Path.GetDirectoryName(strFileName));
                        Shell32.FolderItem folderItem = objFolder.ParseName(System.IO.Path.GetFileName(strFileName));
                        var genre = objFolder.GetDetailsOf(folderItem, 16);

                        if (year.ToString() == "" || Convert.ToInt16(year) == 0 || album == "" || genre.ToString() == "")
                        {
                            var fileshell = ShellFile.FromFilePath(strFileName);
                            ShellPropertyWriter propertyWriter = fileshell.Properties.GetPropertyWriter();

                            try
                            {
                                var trackJSON = SingleEdit.MusixmatchGet(artist, title);

                                if (trackJSON.Item1 != null)
                                {
                                    try
                                    {
                                        string yearMM = "";//trackJSON.Item1.FirstReleaseDate.Year.ToString();
                                        if ((year.ToString() == "" || Convert.ToInt16(year) == 0) && yearMM != "" && Convert.ToInt16(yearMM) <= Convert.ToInt16(DateTime.Now.Year) && Convert.ToInt16(yearMM) > 1900)
                                        {
                                            propertyWriter.WriteProperty(SystemProperties.System.Media.Year, new int[] { Convert.ToInt16(yearMM) });
                                        }
                                    }
                                    catch
                                    {
                                        errorList += Environment.NewLine + "Year error in: " + file.Name;
                                    }
                                    try
                                    {
                                        string albumMM = trackJSON.Item1.AlbumName.ToString();
                                        if (album == "" && albumMM != "")
                                        {
                                            propertyWriter.WriteProperty(SystemProperties.System.Music.AlbumTitle, new string[] { albumMM });
                                        }
                                    }
                                    catch
                                    {
                                        errorList += Environment.NewLine + "Album error in: " + file.Name;
                                    }
                                    if (genre.ToString() == "")
                                    {
                                        try
                                        {
                                            string genreMM = trackJSON.Item1.PrimaryGenres.MusicGenreList[0].MusicGenre.MusicGenreName.ToString();
                                            if (genreMM.ToString() != "")
                                            {
                                                propertyWriter.WriteProperty(SystemProperties.System.Music.Genre, new string[] { genreMM });
                                            }
                                        }
                                        catch (IndexOutOfRangeException)
                                        {
                                            errorList += Environment.NewLine + "Genre error in: " + file.Name;
                                        }
                                    }
                                }
                                else if (trackJSON.Item2 == true)
                                {
                                    break;
                                }
                                else
                                {
                                    errorList += Environment.NewLine + file.Name;
                                }
                            }
                            finally
                            {
                                propertyWriter.Close();
                            }
                        }
                    }
                    progressBar1.Value = Convert.ToInt32(100 * (i + 1) / files.Length);

                    if (i == files.Length - 1)
                    {
                        progressBar1.Value = 100;
                    }
                }
                ErrorList errorForm = new ErrorList(errorList);
                errorForm.ShowDialog();
            }
            catch (FileNotFoundException)
            {
                System.Windows.Forms.MessageBox.Show("Files not found exception!", "Message");
            }
        }
예제 #12
0
        private void ChangeArtistsInfoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                string[] files = Directory.GetFiles(path, "*.mp3", SearchOption.AllDirectories);

                for (int i = 0; i < files.Length; i++)
                {
                    FileInfo file = new FileInfo(files[i]);
                    if (NameCheck(file.Name) == "")
                    {
                        var fileshell = ShellFile.FromFilePath(file.DirectoryName + "\\" + file.Name);

                        Shell32.Shell      shell       = new Shell32.Shell();
                        var                strFileName = file.DirectoryName + "\\" + file.Name;
                        Shell32.Folder     objFolder   = shell.NameSpace(System.IO.Path.GetDirectoryName(strFileName));
                        Shell32.FolderItem folderItem  = objFolder.ParseName(System.IO.Path.GetFileName(strFileName));
                        var                contrArtist = objFolder.GetDetailsOf(folderItem, 13);

                        if (contrArtist == "" || contrArtist.Contains("ntitled") || contrArtist.Contains("various") || contrArtist.Contains("Various") || contrArtist.Contains("nknown") || contrArtist.Contains("http") || contrArtist.Contains("www.") || contrArtist.Contains(".com"))
                        {
                            string   name    = objFolder.GetDetailsOf(folderItem, 0);
                            string[] newName = Regex.Split(name, "-");
                            string[] newest  = Regex.Split(newName[0], "\\.");

                            ShellPropertyWriter propertyWriter = fileshell.Properties.GetPropertyWriter();
                            try
                            {
                                if (newest.Length >= 2 && newest[1] != null)
                                {
                                    propertyWriter.WriteProperty(SystemProperties.System.Music.Artist, new string[] { newest[1] });
                                }
                                else if (newest.Length < 2)
                                {
                                }
                            }
                            finally
                            {
                                propertyWriter.Close();
                            }
                        }
                        ShellObject song        = ShellObject.FromParsingName(strFileName);
                        var         albumArtist = GetValue(song.Properties.GetProperty(SystemProperties.System.Music.AlbumArtist));

                        if (albumArtist == "" || albumArtist.Contains("ntitled") || albumArtist.Contains("various") || albumArtist.Contains("Various") || albumArtist.Contains("nknown") || contrArtist.Contains("nknown") || contrArtist.Contains("http") || contrArtist.Contains("www.") || contrArtist.Contains(".com"))
                        {
                            string   name    = objFolder.GetDetailsOf(folderItem, 0);
                            string[] newName = Regex.Split(name, "-");
                            string[] newest  = Regex.Split(newName[0], "\\.");

                            ShellPropertyWriter propertyWriter = fileshell.Properties.GetPropertyWriter();
                            try
                            {
                                if (newest.Length >= 2 && newest[1] != null)
                                {
                                    propertyWriter.WriteProperty(SystemProperties.System.Music.AlbumArtist, new string[] { newest[1] });
                                }
                                else if (newest.Length < 2)
                                {
                                }
                            }
                            finally
                            {
                                propertyWriter.Close();
                            }
                        }
                    }
                    progressBar1.Value = Convert.ToInt32(100 * (i + 1) / files.Length);

                    if (i == files.Length - 1)
                    {
                        progressBar1.Value = 100;
                    }
                }
            }
            catch (FileNotFoundException)
            {
                System.Windows.Forms.MessageBox.Show("Files not found exception!", "Message");
            }
        }
예제 #13
0
        private void Client_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            if (e.Cancelled)
            {
                HandyControl.Controls.MessageBox.Error("Download Canceled!");
            }
            else
            {
                shDownloadedItem.Status = Convert.ToInt32(shDownloadedItem.Status) + 1;
                // Handle Rename

                ShellFile file = ShellFile.FromFilePath(@Imagepath);

                string isNude = "NOTNUDE";


                //Check Nud

                if (DomainExists(System.IO.Path.GetFileNameWithoutExtension(@JsonPath)))
                {
                    isNude = "ITSNUDE";
                }
                else
                {
                    isNude = "NOTNUDE";
                }

                string date = sig.Substring(sig.LastIndexOf(',') + 1);



                try
                {
                    //Set Attrib
                    ShellPropertyWriter propertyWriter = file.Properties.GetPropertyWriter();
                    propertyWriter.WriteProperty(SystemProperties.System.Title, FixInvalidCharacter(title) ?? "Empty");
                    propertyWriter.WriteProperty(SystemProperties.System.Subject, FixInvalidCharacter(sig) ?? "Empty");
                    propertyWriter.WriteProperty(SystemProperties.System.Comment, FixInvalidCharacter(gal) ?? "Empty");
                    propertyWriter.WriteProperty(SystemProperties.System.Author, FixInvalidCharacter(wikiartist) ?? "Unknown Artist");
                    propertyWriter.WriteProperty(SystemProperties.System.Keywords, new string[] {
                        FixInvalidCharacter(city) ?? "Location Unknown", FixInvalidCharacter(country) ?? "Location Unknown", FixInvalidCharacter(lat.ToString()) ?? "Empty",
                        FixInvalidCharacter(@long.ToString()) ?? "Empty", FixInvalidCharacter(sig) ?? "Empty", FixInvalidCharacter(title) ?? "Empty",
                        FixInvalidCharacter(wikiartist) ?? "Empty", FixInvalidCharacter(gal) ?? "Empty", isNude ?? "Empty", FixInvalidCharacter(date) ?? "Empty"
                    });

                    propertyWriter.Close();

                    string wikiart       = wikiartist ?? "Unknown Artist";
                    string cleanFileName = string.Join("", wikiart.Split(System.IO.Path.GetInvalidFileNameChars()));
                    if (!Directory.Exists(GlobalData.Config.DataPath + @"\" + cleanFileName))
                    {
                        Directory.CreateDirectory(GlobalData.Config.DataPath + @"\" + cleanFileName);
                    }
                    File.Move(@Imagepath, GlobalData.Config.DataPath + @"\" + cleanFileName.Trim() + @"\" + System.IO.Path.GetFileName(@Imagepath));
                }
                catch (Exception)
                {
                }

                DownloadFile();
            }
        }
예제 #14
0
        private void testBtn_Click(object sender, EventArgs e)
        {
            // Write properties to file
            string filePath = @"C:\\Users\Solomon\\Pictures\\Test1.docx";
            var    file     = ShellFile.FromFilePath(filePath);

            // Read and Write:

            string[] oldAuthors = file.Properties.System.Author.Value;
            string   oldTitle   = file.Properties.System.Title.Value;

            file.Properties.System.Comment.Value = "©COPYRIGHT DATAASGUARD";
            file.Properties.System.Title.Value   = "Seolhyun";

            // Alternate way to Write:

            ShellPropertyWriter propertyWriter = file.Properties.GetPropertyWriter();

            //propertyWriter.WriteProperty(SystemProperties.System.Comment, new string[] { "Comment" });
            propertyWriter.Close();

            // Read File extended properties
            List <string> arrHeaders = new List <string>();
            List <Tuple <int, string, string> > attributes = new List <Tuple <int, string, string> >();

            Shell32.Shell shell       = new Shell32.Shell();
            var           strFileName = @"C:\Users\Solomon\Pictures\Test1.docx";

            Shell32.Folder     objFolder  = shell.NameSpace(System.IO.Path.GetDirectoryName(strFileName));
            Shell32.FolderItem folderItem = objFolder.ParseName(System.IO.Path.GetFileName(strFileName));


            for (int i = 0; i < short.MaxValue; i++)
            {
                string header = objFolder.GetDetailsOf(null, i);
                if (String.IsNullOrEmpty(header))
                {
                    break;
                }
                arrHeaders.Add(header);
            }

            // The attributes list below will contain a tuple with attribute index, name and value
            // Once you know the index of the attribute you want to get,
            // you can get it directly without looping, like this:
            var Authors = objFolder.GetDetailsOf(folderItem, 20);

            for (int i = 0; i < arrHeaders.Count; i++)
            {
                var attrName  = arrHeaders[i];
                var attrValue = objFolder.GetDetailsOf(folderItem, i);
                var attrIdx   = i;

                attributes.Add(new Tuple <int, string, string>(attrIdx, attrName, attrValue));
                //if (i <= 5 || i == 10 || i == 21 || i == 24)
                //{
                //Console.WriteLine("{0}\t{1}: {2}", i, attrName, attrValue);
                Console.WriteLine("{0}\t{1}: {2}", i + 1, attrName, attrValue);
                //}
            }
            Console.ReadLine();
        }
예제 #15
0
파일: tag_to_system.cs 프로젝트: Rp70/fvc
        static public void Run(IScripting scripting, string argument)
        {
            scripting.GetConsole().Clear();
            var         catalog   = scripting.GetVideoCatalogService();
            ISelection  selection = scripting.GetSelection();
            List <long> selected  = selection.GetSelectedVideos();

            foreach (long video in selected)
            {
                try
                {
                    var entry = catalog.GetVideoFileEntry(video);

                    IUtilities utilities     = scripting.GetUtilities();
                    var        selected_path = utilities.ConvertToLocalPath(entry.FilePath);

                    var file = ShellFile.FromFilePath(selected_path);

                    var selected_videos = new long[1];
                    selected_videos[0] = video;
                    var           TagInstances = catalog.GetTagsForVideos(selected_videos);
                    List <string> tag_list     = new List <string>();
                    foreach (var tag in TagInstances)
                    {
                        tag_list.Add(tag.Name);
                    }

                    scripting.GetConsole().Write("Tagging : " + selected_path + " ...");
                    ShellPropertyWriter propertyWriter = file.Properties.GetPropertyWriter();
                    propertyWriter.WriteProperty(SystemProperties.System.Keywords, tag_list.ToArray());
                    int Rating = 0;
                    if (entry.Rating == 1)
                    {
                        Rating = 1;
                    }
                    if (entry.Rating == 2)
                    {
                        Rating = 25;
                    }
                    if (entry.Rating == 3)
                    {
                        Rating = 50;
                    }
                    if (entry.Rating == 4)
                    {
                        Rating = 75;
                    }
                    if (entry.Rating == 5)
                    {
                        Rating = 99;
                    }
                    propertyWriter.WriteProperty(SystemProperties.System.Rating, Rating);
                    propertyWriter.WriteProperty(SystemProperties.System.Comment, entry.Description);
                    propertyWriter.Close();

                    scripting.GetConsole().WriteLine("Done ");
                }
                catch (Exception ex)
                {
                    scripting.GetConsole().WriteLine(ex.Message);
                }
            }
        }
예제 #16
0
        public static void SearchAndReplace(string templatePath, string destinationFilePath, Dictionary <string, string> fieldValues, string Author)
        {
            File.Copy(templatePath, destinationFilePath, true);
            FileInfo finfo = new FileInfo(destinationFilePath);

            finfo.Attributes = FileAttributes.Directory | FileAttributes.Normal;

            string docText = "";

            var file = ShellFile.FromFilePath(destinationFilePath);
            ShellPropertyWriter propertyWriter = file.Properties.GetPropertyWriter();

            propertyWriter.WriteProperty(SystemProperties.System.Author, new string[] { Author });
            propertyWriter.WriteProperty(SystemProperties.System.ComputerName, new string[] { "Asia Soft" });
            propertyWriter.Close();

            using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(destinationFilePath, true))
            {
                using (StreamReader sr = new StreamReader(wordDoc.MainDocumentPart.GetStream()))
                {
                    docText = sr.ReadToEnd();
                    sr.Close();
                    sr.Dispose();
                }

                foreach (var item in fieldValues)
                {
                    docText = ReplaceWholeWord(docText, item.Key, item.Value);
                }

                foreach (FooterPart footer in wordDoc.MainDocumentPart.FooterParts)
                {
                    string footerText = null;
                    using (StreamReader sr = new StreamReader(footer.GetStream()))
                    {
                        footerText = sr.ReadToEnd();
                    }

                    foreach (var item in fieldValues)
                    {
                        footerText = ReplaceWholeWord(footerText, item.Key, item.Value);
                    }

                    using (StreamWriter sw = new StreamWriter(footer.GetStream(FileMode.Create)))
                    {
                        sw.Write(footerText);
                    }
                    //Save Header
                    footer.Footer.Save();
                }

                foreach (HeaderPart footer in wordDoc.MainDocumentPart.HeaderParts)
                {
                    string footerText = null;
                    using (StreamReader sr = new StreamReader(footer.GetStream()))
                    {
                        footerText = sr.ReadToEnd();
                    }

                    foreach (var item in fieldValues)
                    {
                        footerText = ReplaceWholeWord(footerText, item.Key, item.Value);
                    }

                    using (StreamWriter sw = new StreamWriter(footer.GetStream(FileMode.Create)))
                    {
                        sw.Write(footerText);
                    }
                    //Save Header
                    footer.Header.Save();
                }



                using (StreamWriter sw = new StreamWriter(wordDoc.MainDocumentPart.GetStream(FileMode.Create)))
                {
                    sw.Write(docText);
                    sw.Close();
                    sw.Dispose();
                }

                wordDoc.Close();
                wordDoc.Dispose();
            }
        }
예제 #17
0
        public void SetData(string location)
        {
            var fileshell = ShellFile.FromFilePath(location);
            ShellPropertyWriter propertyWriter = fileshell.Properties.GetPropertyWriter();

            try
            {
                if (yearTextBox.Text != "" && Convert.ToInt16(yearTextBox.Text) <= Convert.ToInt16(DateTime.Now.Year) && Convert.ToInt16(yearTextBox.Text) > 1900)
                {
                    propertyWriter.WriteProperty(SystemProperties.System.Media.Year, new int[] { Convert.ToInt16(yearTextBox.Text) });
                }
                else if (yearTextBox.Text == "" || Convert.ToInt16(yearTextBox.Text) == 0)
                {
                    propertyWriter.WriteProperty(SystemProperties.System.Media.Year, new int[] { 0 });
                }
                else
                {
                    DialogResult dialogResult = MessageBox.Show("Invalid year!" + Environment.NewLine + "Do you want to continue with the year change?", "Inpropper year!", MessageBoxButtons.YesNoCancel);

                    if (dialogResult == DialogResult.Yes)
                    {
                        propertyWriter.WriteProperty(SystemProperties.System.Media.Year, new int[] { Convert.ToInt16(yearTextBox.Text) });
                    }
                    else if (dialogResult == DialogResult.No)
                    {
                    }
                    else if (dialogResult == DialogResult.Cancel)
                    {
                        return;
                    }
                }

                propertyWriter.WriteProperty(SystemProperties.System.Music.AlbumArtist, new string[] { aArtistTextBox.Text });
                propertyWriter.WriteProperty(SystemProperties.System.Title, new string[] { titleTextBox.Text });
                propertyWriter.WriteProperty(SystemProperties.System.Music.AlbumTitle, new string[] { albumTextBox.Text });
                propertyWriter.WriteProperty(SystemProperties.System.Music.Artist, new string[] { contributingTextBox.Text });
                propertyWriter.WriteProperty(SystemProperties.System.Music.Genre, new string[] { genreBox.Text });
                propertyWriter.WriteProperty(SystemProperties.System.Music.Lyrics, new string[] { lyricsTextBox.Text });
            }
            catch (Exception e)
            {
                MessageBox.Show("Error: " + e, "Setting data error!");
            }
            finally
            {
                propertyWriter.Close();
            }
            try
            {
                string message = Index.NameCheck(nameTextBox.Text);
                if (message == "")
                {
                    System.IO.File.Move(location, System.IO.Path.GetDirectoryName(location) + "\\" + nameTextBox.Text);
                    Close();
                }
                else
                {
                    DialogResult dialogResult = MessageBox.Show("Name check returned: " + message + Environment.NewLine + "Do you want to continue with the name change?", "Inpropper name!", MessageBoxButtons.YesNoCancel);

                    if (dialogResult == DialogResult.Yes)
                    {
                        System.IO.File.Move(location, System.IO.Path.GetDirectoryName(location) + "\\" + nameTextBox.Text);
                        Close();
                    }
                    else if (dialogResult == DialogResult.No)
                    {
                    }
                    else if (dialogResult == DialogResult.Cancel)
                    {
                        Close();
                    }
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("Error: " + e, "Renaming error!");
            }
        }