お世話になります。
2つのファイルの撮影日時、無ければファイル更新日時を比較する関数を作成していますが、
Shell32で撮影日時を取得できた場合に、【string型→DateTime型変換部分】で【エラーメッセージ】が出てしまい、うまくいきません。
撮影日時が無く、更新日時をstring型→DateTime型に変換した場合は問題ありませんでした。
fileX、fileY共に、"yyyy/MM/dd HH:mm"の形式でstring型の日時が取得できています。
(のように見えているだけかもしれませんが…。)
原因がわからずご助言いただけないでしょうか、よろしくお願いいたします。
【string型→DateTime型変換部分】
c#
1DateTime datetimeX = 2 DateTime.ParseExact(stringX, "yyyy/MM/dd HH:mm", DateTimeFormatInfo.InvariantInfo, DateTimeStyles.None); 3DateTime datetimeY = 4 DateTime.ParseExact(stringY, "yyyy/MM/dd HH:mm", DateTimeFormatInfo.InvariantInfo, DateTimeStyles.None);
【エラーメッセージ】
c#
1System.FormatException: '文字列は有効な DateTime ではありませんでした。' 2
【関数全ソース】
c#
1 2static int Compare(string fileX, string fileY) 3{ 4 Shell32.Shell shell = new Shell32.Shell(); 5 6 Shell32.Folder objFolder_photX = shell.NameSpace(Path.GetDirectoryName(fileX)); 7 Shell32.FolderItem folderItem_photX = objFolder_photX.ParseName(Path.GetFileName(fileX)); 8 string stringX = objFolder_photX.GetDetailsOf(folderItem_photX, 12); //撮影日時 9 10 Shell32.Folder objFolder_photY = shell.NameSpace(Path.GetDirectoryName(fileY)); 11 Shell32.FolderItem folderItem_photY = objFolder_photY.ParseName(Path.GetFileName(fileY)); 12 string stringY = objFolder_photY.GetDetailsOf(folderItem_photY, 12); //撮影日時 13 14 if (stringX == "") 15 { 16 Shell32.Folder objFolder = shell.NameSpace(Path.GetDirectoryName(fileX)); 17 Shell32.FolderItem folderItem = objFolder.ParseName(Path.GetFileName(fileX)); 18 stringX = objFolder.GetDetailsOf(folderItem, 3); //更新日時 19 } 20 if(stringY == "") 21 { 22 Shell32.Folder objFolder = shell.NameSpace(Path.GetDirectoryName(fileY)); 23 Shell32.FolderItem folderItem = objFolder.ParseName(Path.GetFileName(fileY)); 24 stringY = objFolder.GetDetailsOf(folderItem, 3); //更新日時 25 } 26 27 DateTime datetimeX = 28 DateTime.ParseExact(stringX, "yyyy/MM/dd HH:mm", DateTimeFormatInfo.InvariantInfo, DateTimeStyles.None); 29 DateTime datetimeY = 30 DateTime.ParseExact(stringY, "yyyy/MM/dd HH:mm", DateTimeFormatInfo.InvariantInfo, DateTimeStyles.None); 31 32 return DateTime.Compare(datetimeX, datetimeY); 33}

回答4件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/12/10 09:16