我一直在检查,试图找到一种写入exe.config文件的方法。
我可以很好地读取/写入区域,但由于我们使用来自siemens的2个不同的DLL来自动化一些东西,我需要在exe.config文件的不同部分添加一堆路径。
这是配置在结构中的样子。
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="Siemens.Engineering.dll" value="C:\Program Files (x86)\Siemens\Automation\Portal V13\PublicAPI\V13 SP1\Siemens.Engineering.dll" />
<add key="Siemens.Engineering.Hmi.dll" value="C:\Program Files (x86)\Siemens\Automation\Portal V13\PublicAPI\V13 SP1\Siemens.Engineering.Hmi.dll" />
<add key="TempDirectory" value="D:\Scania Oskarshamn\Alarm_text" />
<add key="AlarmTextPath" value="AlarmTexts" />
<add key="Auto.Compile" value="True" />
<add key="Auto.ExtractAlarms" value="True" />
<add key="Auto.ImportAlarms" value="True" />
<add key="Compile.HMI" value="changes" />
<add key="Lang1" value="sv-SE:Swedish (Sweden)" />
<add key="Lang2" value="en-GB:English (United Kingdom)" />
<add key="Lang3" value="pt-BR:Portugese (Brazil)" />
<add key="LangChecked" value="sv-SE:en-GB" />
</appSettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Siemens.Engineering" culture="neutral" publicKeyToken="d29ec89bac048f84"/>
<codeBase version="1.2.0.0" href="FILE://C:\Program Files (x86)\Siemens\Automation\Portal V13\PublicAPI\V13 SP1\Siemens.Engineering.dll"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Siemens.Engineering.Hmi" culture="neutral" publicKeyToken="37b6e3a80df0900f"/>
<codeBase version="1.2.0.0" href="FILE://C:\Program Files (x86)\Siemens\Automation\Portal V13\PublicAPI\V13 SP1\Siemens.Engineering.Hmi.dll"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
我需要更改标签中的href部分,如果此部分不存在,我还需要添加下面的整个结构。 我只需要写下这些部分。
编辑:现在我甚至不需要配置中的路径,使用注册表查找路径并使用AssemblyResolve使其工作。 通过使用:
解决了这个问题 public frmAutomate(AutoData autoData)
{
InitializeComponent();
AppDomain CurrentDomain = AppDomain.CurrentDomain;
CurrentDomain.AssemblyResolve += new ResolveEventHandler(MyResolver);
}
private static Assembly MyResolver(object sender, ResolveEventArgs args)
{
int index = args.Name.IndexOf(',');
if (index == -1)
{
return null;
}
string name = args.Name.Substring(0, index) + ".dll";
// Check for 64bit installation
RegistryKey filePathReg = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Wow6432Node\\Siemens\\Automation\\_InstalledSW\\TIAP13\\TIA_Opns");
// Check for 32bit installation
if (filePathReg == null)
filePathReg = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Siemens\\Automation\\_InstalledSW\\TIAP13\\TIA_Opns");
string filePath = filePathReg.GetValue("Path").ToString() + "PublicAPI\\V13 SP1";
string path = Path.Combine(filePath, name);
// User must provide the correct path
string fullPath = Path.GetFullPath(path);
if (File.Exists(fullPath))
{
return Assembly.LoadFrom(fullPath);
}
return null;
}