这个问题可能比较局部化,但我确实需要另一个人对我在这里做错了什么发表意见。当整个过程的每个阶段都看起来正常无误时,我如何在路径中传递非法字符到临时文件呢?
我收到了以下错误信息:
文件存在于指定的位置(在执行期间已经检查过),但是System.IO.File.Exists认为不存在,我无法看到任何明显的问题。有什么解决方法吗?
更多代码可根据要求提供。
REQ1:你的路径是如何声明的?
会话变量被传递到各个位置,但不会被更改。 REQ2: 你真的在使用
基本数据持有类:
我收到了以下错误信息:
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.ArgumentException: Illegal characters in path.
将这个:
"C:\Documents and Settings\<username>\Local Settings\Temp\1\tmp1E0.tmp"
转换为这个:
XmlDocument doc = new XmlDocument();
doc.Load(<above string>);
文件存在于指定的位置(在执行期间已经检查过),但是System.IO.File.Exists认为不存在,我无法看到任何明显的问题。有什么解决方法吗?
更多代码可根据要求提供。
REQ1:你的路径是如何声明的?
try
{
session.TempConfigDir = System.IO.Path.GetTempFileName();
//All work done with the temp file happens within the Form
Form currentform = new WelcomeDialog(session);
DialogResult dr = currentform.ShowDialog();
}
finally
{
File.Delete(session.TempConfigDir);
}
会话变量被传递到各个位置,但不会被更改。 REQ2: 你真的在使用
<username>
吗?
不,我已经编辑掉了。它是一个有效的Windows用户名。
REQ3: 调试时你得到了什么?
实际上这是在安装程序中发生的(稍微难以物理调试),但上面的字符串是我从日志中获取的一个示例,当然,其中包括有效的用户名。
REQ4: 更多关于如何使用的代码?
我添加了WiX标记,因为这涉及到WiX3.7。基本数据持有类:
public class SessionState
{
//<other properties>
public string TempConfigDir { get; set; }
public SessionState()
{
//Setting of properties
}
}
在表单内部:
//StringBuilder for arguments
installerargs.Append("\" TEMPCONFIGDIR=\"");
installerargs.Append(m_Session.TempConfigDir);
//...
Process p = Process.Start("msiexec", installerargs.ToString());
p.WaitForExit();
追加:表单中缺失的部分:
//It's grabbing the web.config from an existing install
//and copying it over the temp file, not changing its location or name.
File.Copy(m_Session.INSTALLDIR + DIR_WEB_CONFIG, m_Session.TempConfigDir, true);
在WiX3.7的MSI中:
<Property Id="TEMPCONFIGDIR" Value="UNSET" />
...
<Custom Action="CA_InstallUICA.SetProp" After="StartServices">NOT Installed</Custom>
<Custom Action="CA_InstallUICA" After="CA_InstallUICA.SetProp">NOT Installed</Custom>
...
<CustomAction Id="CA_InstallUICA.SetProp" Property="CA_InstallUICA" Value="rcswebdir=[MCWSVDIR];webdir=[WEBAPPVDIR];installtype=notransaction;targetdir=[INSTALLDIR];interaction=[INTERACTION];tempconfigdir="[TEMPCONFIGDIR]";" />
从使用它的自定义操作中:
wz.AutoSettings.TempConfigLocation = session.CustomActionData["tempconfigdir"];
//Where I get the above string passed out
session.Log(wz.AutoSettings.TempConfigLocation);
//The rest of the code that uses it is above and where the exception is thrown
REQ5: 你是否将TempConfigDir变量更改为something.xml?
不,我复制一个XML文件到提供的确切名称/目录(包括.tmp)。
REQ6: 你确定它是在.Load()上发生吗?
是的,我已记录了该行两侧,并且仅在执行时击中第一行。
Path.GetTempFileName()
创建的,那么我不确定发生了什么。您是否进行了调试并确认在使用doc.Load
时没有转换/修改该路径? - Chris Sinclair