Ejemplo n.º 1
0
        private bool _AnalyzeFile(ref FileData f)
        {
            bool retVal = false;

            f.FileType = Path.GetExtension(f.FilePath);
            f.FileName = Path.GetFileName(f.FilePath);

            // Create file data
            try
            {
                // File infos
                FileInfo fi = new FileInfo(f.FilePath);
                f.DirectoryName = fi.DirectoryName;
                f.FileSize      = fi.Length;
                f.DateTaken     = fi.LastWriteTime; //fi.CreationTime;
                f.CameraType    = "Unknown";
                fi = null;

                // Get image meta data
                if (f.IsImage() || f.IsVideo())
                {
                    if (f.IsImage())
                    {
                        using (FileStream fs = new FileStream(f.FilePath, FileMode.Open, FileAccess.Read))
                            using (Image myImage = Image.FromStream(fs, false, false))
                            {
                                PropertyItem propItem;
                                string       dateTaken       = string.Empty;
                                DateTime     dateTakenFormat = new DateTime();
                                try
                                {
                                    propItem        = myImage.GetPropertyItem(0x9003);//36867); //PropertyTagExifDTOrig
                                    dateTaken       = r.Replace(Encoding.UTF8.GetString(propItem.Value), "-", 2);
                                    dateTakenFormat = DateTime.Parse(dateTaken);
                                }
                                catch
                                {
                                }
                                if (dateTaken != null && dateTakenFormat.Year > 1970)
                                {
                                    f.DateTaken = dateTakenFormat;
                                }

                                ASCIIEncoding encodings  = new ASCIIEncoding();
                                string        cameraType = string.Empty;
                                try
                                {
                                    propItem   = myImage.GetPropertyItem(0x0110);//PropertyTagEquipModel;
                                    cameraType = encodings.GetString(propItem.Value);
                                    //cameraType = cameraType.Substring(0,cameraType.Length);
                                }
                                catch
                                {
                                }
                                if (!string.IsNullOrEmpty(cameraType))
                                {
                                    f.CameraType = cameraType;
                                }

                                //fs.Dispose();
                                //myImage.Dispose();
                            }
                    }

                    string dTold = f.DateTaken.ToString();
                    if (f.ConvertFileName2DateTaken(OldNameFile.Text))
                    {
                        ShowMessage($"DateTaken changed {dTold} -> {f.DateTaken.ToString()}");
                    }

                    // Create new name
                    _GetNewFileName(ref f);

                    if (!f.FilePath.Equals(f.FilePathNew))
                    {
                        Invoke((Action) delegate { FilesToMove.Value++; });

                        ShowMessage($"New name {f.FilePathNew.Replace(FolderPathOutput.Text, "")}");

                        // Add file to collection
                        FilesInFolder.Add(f);
                        retVal = true;
                    }
                }
                else // other files
                {
                    if (!CheckedJustRename.Checked && MoveOtherFiles.Checked)
                    {
                        f.FileNameNew = f.FileName;
                        string dirNew = Toolbox.CombinePathAndFileName(FolderPathOutput.Text, "Others");
                        f.DirectoryNameNew = Toolbox.CombinePathAndFileName(dirNew, f.FileType.Replace(".", ""));
                        System.IO.Directory.CreateDirectory(f.DirectoryNameNew);

                        f.FilePathNew = Toolbox.CombinePathAndFileName(f.DirectoryNameNew, f.FileNameNew);
                        retVal        = true;
                    }

                    // Add file to collection
                    _OtherFilesCounter++;
                    OtherFilesInFolder.Add(f);
                }
            }
            catch (Exception e)
            {
                ShowMessage($"File access failed. {f.FilePath.Replace(FolderPathInput.Text, "")}", LogImage.Error, e);
            }
            return(retVal);
        }