开发者

Deploy an application's xml file with installer or create it on the fly if it does not exist

I am having an xml file like:

<CurrentProject>
// Elements like 
// last opened project file to reopen it when app starts
// and more global project independend settings
</CurrentProject>

Now I asked myself wether I should deliver this xml file with above empty elements with the installer for my app or should I create this file on the fly on a开发者_如何学Pythonpplication start if it does not exist else read the values from it.

Consider also that the user could delete this file and that should my application not prevent from working anymore.

What is better and why?

UPDATE:

What I did felt ok for me so I post my code here :) It just creates the xml + structure on the fly with some security checks...

public ProjectService(IProjectDataProvider provider)
       {
           _provider = provider;   

           string applicationPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
           _projectPath = Path.Combine(applicationPath,@"TBM\Settings.XML");

           if (!File.Exists(_projectPath))
           {
               string dirPath = Path.Combine(applicationPath, @"TBM");

               if (!Directory.Exists(dirPath))
                   Directory.CreateDirectory(dirPath);

               using (var stream = File.Create(_projectPath))
               {
                   XElement projectElement = new XElement("Project");
                   projectElement.Add(new XElement("DatabasePath"));

                   projectElement.Save(stream, SaveOptions.DisableFormatting);   
               }   
           }                
       } 


In a similar scenario, I recently went for creating the initial file on the fly. The main reason I chose this was the fact that I wasn't depending on this file being there and being valid. As this was a file that's often read from/written to, there's a chance that it could get corrupted (e.g. if the power is lost while the file is being written).

In my code I attempted to open this file for reading and then read the data. If anywhere during these steps I encountered an error, I simply recreated the file with default values and displayed a corresponding message to the user.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜