Click here to Skip to main content
15,881,559 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to write to the xml file from my add-in c# for outlook 2010.

I use VSTO 2010.

MyFile.config :
XML
<?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <appSettings>
            <add key="Test1" value="My value 1"/>
        </appSettings>
    </configuration>


So I want to change the value of Test1 (key = "Test1") from my add-in c# for outlook 2010.

MyAdd-in.cs

C#
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
    XmlDocument doc = new XmlDocument();
    doc.Load(Assembly.GetExecutingAssembly().Location + ".config");

    // To write in MyFile.config

    // retrieve appSettings node
    XmlNode node = doc.SelectSingleNode("//appSettings");

    // Select the 'add' element that contains the key
    XmlElement elem =  (XmlElement)node.SelectSingleNode(string.Format("//add[@key='{0}']", "Test1"));
    if (elem != null)
    {
        // add value for key
        elem.SetAttribute("value", "TestValue");
    }
    else
    {
        // Key was not found so create the 'add' element and set it's key/value attributes
        elem = doc.CreateElement("add");
        elem.SetAttribute("key", "Test1");
        elem.SetAttribute("value", "TestValue");
        node.AppendChild(elem);
     }

    doc.Save(Assembly.GetExecutingAssembly().Location + ".config");
}


So it doesn't work and the problem is [Assembly.GetExecutingAssembly().Location + ".config"] !!!

Can you help me please ?

Thank you in advance.
Posted
Comments
Suvabrata Roy 5-Aug-14 8:45am    
Please specify the error.
But I have better way to write App Config.
Member 10872485 5-Aug-14 9:12am    
In :

catch (FileNotFoundException e)
{
throw new Exception("No configuration file found. ", e);
}

return :

No configuration file found !
Suvabrata Roy 5-Aug-14 10:00am    
so when you run it as a add-in or in time of debug.
Member 10872485 6-Aug-14 1:49am    
Hello, I have this problem when I run an add-in !!!
Member 10872485 6-Aug-14 4:17am    
So what can I do please ?

1 solution

Hi,

To Change App Config

C#
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); 
    config.AppSettings.Settings["test"].Value = "blah";       
    config.Save(ConfigurationSaveMode.Modified);
    ConfigurationManager.RefreshSection("appSettings");


but In your case as you are using Add-In I think Save JSON/XML in AppData would be more applicable and fissile.

How to do it...

get AppData Folder

var folderName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData),"OutlookAddin");


Create folder if don't exists now create your desire XML File or JSON file there and Read and write. I would suggest you to use serialization to create or modify those configuration details at AppData folder.

How to Serialize Class: Object Serialization using C#[^]

using JSON : JSON Serialization and Deserialization in ASP.NET[^] ( Just watch how to serialize a .Net class to JSON)

I was busy with some deployment at my end that's why I did not reply your answer sorry for delay.

If you need any further clarification please feel free to ask.
 
Share this answer
 
v2
Comments
Member 10872485 7-Aug-14 1:59am    
Thank you very much Suvabrata Roy, I appreciate your response.

I can save save the setup data by writing into MyFile.xml and not App.config, it is another solution.

// Create Config.xml
XmlDocument doc = new XmlDocument();
// Create :
XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
doc.AppendChild(docNode);

// Create : <setup>
XmlNode App4LegalSetup = doc.CreateElement("Setup");
doc.AppendChild(Setup);
...
doc.Save(@"C:\config.xml");
...
// Change a text of the node
string targetNode = "x";
string targetNodeText = "y";
doc.Load(@"C:\config.xml");
XmlNodeList Nodes = doc.GetElementsByTagName(targetNode);
Nodes[0].InnerText = targetNodeText;
doc.Save(@"C:\config.xml");
...

What is your opinion ?
Suvabrata Roy 7-Aug-14 2:43am    
You can but you need to write more code rather than serialization another suggestion don't save config at C: or root drive its not recommended I would suggest you to save it under CommonApplicationData Folder
Member 10872485 7-Aug-14 3:17am    
Thank you, you're right not config at C, of course I want to avoid that in my project.
It is very interesting to learn from your experience :)
If I need any questions or any further clarification, you prefer to contact you on this site or you have another personal website ?
Thank you thank you :)
Suvabrata Roy 7-Aug-14 4:40am    
Thanks, Its my pleasure that my experience helps you.
I have a website : suvabrataroy.com
but I am still constructing that so you can post on Codeproject.
Member 10872485 14-Aug-14 3:48am    
Hi Suvabrata Roy, can you help me about this question please :
http://www.codeproject.com/Questions/807354/How-to-deserialize-json-array-in-csharp

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900