How to use app.config with Visual Studio add-in?

一世执手 提交于 2019-12-08 06:07:15

问题


I'm building a simple VS2008 add-in. What is the best practice for storing custom run-time settings? Where do you store the app.config and how do you access it from the add-in?


回答1:


Try something like this with System.IO.IsolatedStorageFile (haven't tested sample code.. it's just to show the idea)

Writing

using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForAssembly())
    {
       using (StreamWriter stream = new StreamWriter(new IsolatedStorageFileStream("YourConfig.xml", FileMode.Create, file)))
       {
          stream.Write(YourXmlDocOfConfiguration);
       }
       stream.Flush();
       stream.Close();
     }

Reading

 string yourConfigXmlString;
    using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForAssembly())
    {
       string[] files = file.GetFileNames("YourConfig.Xml");
       if (files.Length > 0 && files[0] == "YourConfig.xml"))
       {
          using (StreamReader stream = new StreamReader(new IsolatedStorageFileStream("YourConfig.xml", FileMode.Open, file)))
          { 
            yourConfigXmlString = stream.ReadToEnd();
          }             
       }
     }


来源:https://stackoverflow.com/questions/344024/how-to-use-app-config-with-visual-studio-add-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!