路径中的非法字符之谜

3
这个问题可能比较局部化,但我确实需要另一个人对我在这里做错了什么发表意见。当整个过程的每个阶段都看起来正常无误时,我如何在路径中传递非法字符到临时文件呢?
我收到了以下错误信息:
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=&quot;[TEMPCONFIGDIR]&quot;;" />

从使用它的自定义操作中:

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()上发生吗?

是的,我已记录了该行两侧,并且仅在执行时击中第一行。


3
我猜想您的字符串实际上并没有包含"<username>",因为">"和"<"是路径中无效的字符。;) - Thomas Levesque
1
@VictorMukherjee:我考虑过这个问题,但实际上,那个字符串根本无法编译,因为它包含了未被识别的转义序列(\D、\S、\L、\T、\1)。 - Chris Sinclair
1
如果您的文件路径是直接通过Path.GetTempFileName()创建的,那么我不确定发生了什么。您是否进行了调试并确认在使用doc.Load时没有转换/修改该路径? - Chris Sinclair
1
如果这个异常是从安装程序中发生的,你怎么确定这个异常是由xmlDoc.Load()方法引发的? - EkoostikMartin
2
这让我想知道:“tempconfigdir="[TEMPCONFIGDIR]"”...路径已经被引用了吗,因此需要将引号加倍?还是在命令行调用中添加更多引号? - DonBoitnott
显示剩余11条评论
1个回答

2

这行代码看起来有问题:

<CustomAction Id="CA_InstallUICA.SetProp" Property="CA_InstallUICA" Value="rcswebdir=[MCWSVDIR];webdir=[WEBAPPVDIR];installtype=notransaction;targetdir=[INSTALLDIR];interaction=[INTERACTION];tempconfigdir=&quot;[TEMPCONFIGDIR]&quot;;" />

该部分引用路径的可能会将引号加倍,从而导致异常:
tempconfigdir=&quot;[TEMPCONFIGDIR]&quot;

去掉&quote;包装以获取实际路径:

tempconfigdir=[TEMPCONFIGDIR]

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接