msi触发的程序无法在网络卷上创建xml文件



我有一个HMI程序,它使用xml文件进行一些机器特定的设置。当HMI在机器网络中的计算机上运行时,这些xml文件存储在机器本身上。机器hdd上的一个文件夹映射为HMI计算机中的一个网络卷,这是xml文件所在的位置。在安装HMI期间,会运行一个小程序来验证xml文件是否存在,如果不是,则会创建默认文件。

我遇到的问题是,当这个小程序由安装程序包运行时,卷似乎不存在,但如果我从visual studio手动运行程序或单独执行编译后的exe文件,则卷被识别。

这可能是msi包触发程序执行的一些问题吗?

using System;
using System.IO;
using System.Xml;
/// <summary>
/// This is executed during installation of the hmi to verify the presence of plcsettings.xml on the machine.
/// If a file is missing a default file is added which must be edited manually after intallation is complete.
/// </summary>
namespace Install.Xml
{
class Program
{
#region fields
private const string plcSettingsPath = @"SettingsHMI SettingsPlcSettings.xml";
private const string heatExPlcSettingsPath = @"SettingsHMI SettingsHeatExPlcSettings.xml";
private const string xmlDirectoryPath = @"SettingsHMI Settings";
private const string volume = @"Y:";
#endregion
/// <summary>
/// Entrypoint for the installer.
/// </summary>
/// <param name="args">Not used.</param>
static void Main(string[] args)
{
Console.WriteLine("Starting installation checks..");
Console.WriteLine();
InstallXmlFiles();
Console.WriteLine();
Console.WriteLine("Done!");
}
/// <summary>
/// Main logic.
/// </summary>
private static void InstallXmlFiles()
{
if (CheckIfXmlDirectoryExists())
{
Console.WriteLine("Installing xml settings file(s)..");
if (!File.Exists(volume + plcSettingsPath))
CreatePlcSettingsXml();
else
Console.WriteLine("PLC Settings already present, skipping..");
if (!File.Exists(volume + heatExPlcSettingsPath))
CreateHeatExPlcSettingsXml();
else
Console.WriteLine("HeatEx PLC Settings already present, skipping..");
}
else
{
Console.WriteLine(volume + xmlDirectoryPath);
Console.WriteLine("The directory for xml settings does not exist or is un-available.");
Console.WriteLine("The installation will continue but the files must be added manually.");
Console.WriteLine();
Console.Write("Press any key to continue..");
Console.ReadKey();
}
}
/// <summary>
/// Checks xml path and creates directory if missing.
/// </summary>
/// <returns></returns>
private static bool CheckIfXmlDirectoryExists()
{
if (!Directory.Exists(volume))
return false;
Directory.CreateDirectory(volume + xmlDirectoryPath);
return true;
}
/// <summary>
/// Checks path and creates file if missing.
/// </summary>
private static void CreateHeatExPlcSettingsXml()
{
XmlDocument xDoc = new XmlDocument();
xDoc.LoadXml("<?xml version="1.0" encoding="utf-8" ?><root><field plcnetid="000.000.000.000.0.0" plcport="801" TcVersion="V2"/></root >");
xDoc.Save(volume + heatExPlcSettingsPath);
Console.WriteLine("Default HeatEx PLC settings installed!");
}
/// <summary>
/// Checks path and creates file if missing.
/// </summary>
private static void CreatePlcSettingsXml()
{
XmlDocument xDoc = new XmlDocument();
xDoc.LoadXml("<?xml version="1.0" encoding="utf-8" ?><root><field plcnetid="000.000.000.000.0.0" machineId="DuoSystem #000" databaseIp="192.168.100.0" duomode="Standard" NewMfcSetting="false" useopcua="false" plcport="801" TcVersion="V2"/></root>");
xDoc.Save(volume + plcSettingsPath);
Console.WriteLine("Default PLC settings installed!");
}
}
}

编辑::

我已经修改了安装程序,在安装程序本身中使用类重写来使用上述逻辑。这是新的实现。

using System;
using System.Collections;
using System.ComponentModel;
using System.IO;
using System.Windows;
using System.Xml;
namespace DuoSystem.Installer
{
[RunInstaller(true)]
public partial class CustomActions : System.Configuration.Install.Installer
{
#region On After Install
protected override void OnAfterInstall(IDictionary savedState)
{
base.OnAfterInstall(savedState);
XmlInstall xmlInstall = new XmlInstall();
xmlInstall.Start();
}
#endregion
#region On After Uninstall
protected override void OnAfterUninstall(IDictionary savedState)
{       
base.OnAfterUninstall(savedState);
}
#endregion
}
#region Supporting Classes
class XmlInstall
{
#region fields
private const string plcSettingsPath = @"SettingsHMI SettingsPlcSettings.xml";
private const string heatExPlcSettingsPath = @"SettingsHMI SettingsHeatExPlcSettings.xml";
private const string xmlDirectoryPath = @"SettingsHMI Settings";
private const string volume = @"Y:";
#endregion
/// <summary>
/// Entrypoint for the installer.
/// </summary>
/// <param name="args">Not used.</param>
public void Start()
{
InstallPlcSettingsXml();
}
/// <summary>
/// Main logic.
/// </summary>
private void InstallPlcSettingsXml()
{
if (CheckIfXmlDirectoryExists())
{   
if (!File.Exists(volume + plcSettingsPath))
CreatePlcSettingsXml();
if (!File.Exists(volume + heatExPlcSettingsPath))
CreateHeatExPlcSettingsXml();
}
else
{

MessageBox.Show( volume + xmlDirectoryPath 
+ Environment.NewLine
+ "The directory for xml settings does not exist or is un-available."
+ Environment.NewLine
+ "The installation will continue but the files must be added manually."
+Environment.NewLine); 
}
}
/// <summary>
/// Checks xml path and creates directory if missing.
/// </summary>
/// <returns></returns>
private bool CheckIfXmlDirectoryExists()
{
if (!Directory.Exists(volume))
return false;
Directory.CreateDirectory(volume + xmlDirectoryPath);
return true;
}
/// <summary>
/// Checks path and creates file if missing.
/// </summary>
private void CreateHeatExPlcSettingsXml()
{
XmlDocument xDoc = new XmlDocument();
xDoc.LoadXml("<?xml version="1.0" encoding="utf-8" ?><root><field plcnetid="000.000.000.000.0.0" plcport="801" TcVersion="V2"/></root >");
xDoc.Save(volume + heatExPlcSettingsPath);
Console.WriteLine("Default HeatEx PLC settings installed!");
}
/// <summary>
/// Checks path and creates file if missing.
/// </summary>
private void CreatePlcSettingsXml()
{
XmlDocument xDoc = new XmlDocument();
xDoc.LoadXml("<?xml version="1.0" encoding="utf-8" ?><root><field plcnetid="000.000.000.000.0.0" machineId="DuoSystem #000" databaseIp="192.168.100.0" duomode="Standard" NewMfcSetting="false" useopcua="false" plcport="801" TcVersion="V2"/></root>");
xDoc.Save(volume + plcSettingsPath);
Console.WriteLine("Default PLC settings installed!");
}
}
#endregion
}

这是一个更漂亮的实现,我希望它能同时解决下面提到的可能的模拟问题,然而它没有。行为仍然是相同的。

您没有分享程序是如何被安装程序调用的,但我的猜测是您在没有模拟的情况下调用它,以便它在系统上下文中运行。在这种情况下,它没有作为(模拟)登录用户运行,也没有访问网络资源的权限。

最新更新