public void WriteConfiguration(string filePath, SecuredApplication configuration)
{
if (configuration == null) throw new ArgumentNullException("configuration");
// check for valid file.
if (String.IsNullOrEmpty(filePath) || !File.Exists(filePath))
{
throw ServiceResultException.Create(
StatusCodes.BadNotReadable,
"Cannot find the configuration file: {0}",
configuration.ConfigurationFile);
}
// load from file.
XmlDocument document = new XmlDocument();
document.Load(new FileStream(filePath, FileMode.Open));
XmlElement element = Find(document.DocumentElement, "SecuredApplication", Namespaces.OpcUaSecurity);
// update secured application.
if (element != null)
{
configuration.LastExportTime = DateTime.UtcNow;
element.InnerXml = SetObject(typeof(SecuredApplication), configuration);
}
// update application configuration.
else
{
UpdateDocument(document.DocumentElement, configuration);
}
try
{
// update configuration file.
Stream ostrm = File.Open(filePath, FileMode.Create, FileAccess.Write);
StreamWriter writer = new StreamWriter(ostrm, System.Text.Encoding.UTF8);
try
{
document.Save(writer);
}
finally
{
writer.Flush();
writer.Dispose();
}
}
catch (Exception e)
{
throw ServiceResultException.Create(
StatusCodes.BadNotWritable,
e,
"Cannot update the configuration file: {0}",
configuration.ConfigurationFile);
}
}
#endregion