@@ -17,13 +17,58 @@ public static void SavePost(BlogPost post, string filePath)
1717 private static void SavePostAsStructuredStorage ( BlogPost post , string filePath )
1818 {
1919 ApplicationEnvironment . Initialize ( Assembly . GetExecutingAssembly ( ) ,
20- Path . Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . ProgramFiles ) , @"\ Windows Live\Writer\ " ) ) ;
20+ Path . Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . ProgramFiles ) , @"Windows Live\Writer" ) ) ;
2121
2222 PostEditorFile . Initialize ( ) ;
2323
24- PostEditorFile pef = PostEditorFile . CreateNew ( new DirectoryInfo ( Path . GetDirectoryName ( filePath ) ) ) ;
24+ string ? directoryPath = Path . GetDirectoryName ( filePath ) ;
25+ if ( string . IsNullOrEmpty ( directoryPath ) )
26+ {
27+ directoryPath = Directory . GetCurrentDirectory ( ) ;
28+ }
29+
30+ DirectoryInfo targetDirectory = new DirectoryInfo ( directoryPath ) ;
31+ string [ ] existingDrafts = Directory . GetFiles ( targetDirectory . FullName , "*.wpost" ) ;
32+
33+ PostEditorFile pef = PostEditorFile . CreateNew ( targetDirectory ) ;
2534 var bef = new BlogPostEditingContext ( post . Id , post ) ;
2635 pef . SaveBlogPost ( bef ) ;
36+
37+ string createdDraftPath = FindCreatedDraftPath ( targetDirectory . FullName , existingDrafts ) ;
38+ if ( ! string . Equals ( createdDraftPath , filePath , StringComparison . OrdinalIgnoreCase ) )
39+ {
40+ if ( File . Exists ( filePath ) )
41+ {
42+ File . Delete ( filePath ) ;
43+ }
44+
45+ File . Move ( createdDraftPath , filePath ) ;
46+ }
47+ }
48+
49+ private static string FindCreatedDraftPath ( string directoryPath , string [ ] existingDrafts )
50+ {
51+ string [ ] currentDrafts = Directory . GetFiles ( directoryPath , "*.wpost" ) ;
52+
53+ foreach ( string currentDraft in currentDrafts )
54+ {
55+ bool existedBefore = false ;
56+ foreach ( string existingDraft in existingDrafts )
57+ {
58+ if ( string . Equals ( currentDraft , existingDraft , StringComparison . OrdinalIgnoreCase ) )
59+ {
60+ existedBefore = true ;
61+ break ;
62+ }
63+ }
64+
65+ if ( ! existedBefore )
66+ {
67+ return currentDraft ;
68+ }
69+ }
70+
71+ throw new IOException ( $ "Unable to determine the draft file created in '{ directoryPath } '.") ;
2772 }
2873
2974
0 commit comments