字符串中不需要的转义字符

时间:2009-12-08 15:22:29

标签: c# escaping character

App.Config文件内容:

    <?xml version="1.0" encoding="utf-8" ?>
<configuration>

    <appSettings>
        <add key="ConnectionString"
             value="Server=ADVDSQLMGA;User ID=ImporterApp;Password=!mp0rt3rApp;Database=id0405Moxy52"
             />


        <add key="DTS_PackageName"
            value="BlockImportNEW"/>
        <add key="DTS_PackagePathAndFileName"
            value="\\computername\foldername\folder1\folder2\folder3\filename.dts"/>
        <add key="DTS_PackageGUID"
            value="{C22A80D9-7613-43AF-939C-3C04AD7D848A}"/>
        <add key="DTS_ImportSourcePath"
            value="C:\foldername"/>
        <add key="DTS_ImportFileConnection"
             value="'filename.txt';'';'0'"/>
        <add key="DTS_ImportFileName"
             value="filename.txt"/>
        <add key="DTS_ImportWorkingPath"
             value="\\computername\foldername\folder1\folder2"/>
        <add key="DTS_ImportWorkingPathAndFileName"
             value="\\computername\foldername\folder1\folder2\filename.txt"/>
        <add key="DTS_DestinationServerName"
             value="computername"/>
        <add key="DTS_DestinationDatabase"
             value="databasename"/>
        <add key="DTS_DestinationTable"
             value="databasename.dbo.tablename"/>
        <add key="DTS_DestinationUserName"
             value="username"/>
        <add key="DTS_DestinationPassword"
             value="password"/>
    </appSettings>


</configuration>

代码:

            string DTS_PackageName = ConfigurationSettings.AppSettings["DTS_PackageName"];
            string DTS_PackagePathAndFileName = ConfigurationSettings.AppSettings["DTS_PackagePathAndFileName"];
            string DTS_PackageGUID = ConfigurationSettings.AppSettings["DTS_PackageGUID"];
            string DTS_ImportSourcePath = ConfigurationSettings.AppSettings["DTS_ImportSourcePath"];
            string DTS_ImportFileConnection = ConfigurationSettings.AppSettings["DTS_ImportFileConnection"];
            string DTS_ImportFileName = ConfigurationSettings.AppSettings["DTS_ImportFileName"];
            string DTS_ImportWorkingPath = ConfigurationSettings.AppSettings["DTS_ImportWorkingPath"];
            string DTS_ImportWorkingPathAndFileName = ConfigurationSettings.AppSettings["DTS_ImportWorkingPathAndFileName"];
            string DTS_DestinationServerName = ConfigurationSettings.AppSettings["DTS_DestinationServerName"];
            string DTS_DestinationDatabase = ConfigurationSettings.AppSettings["DTS_DestinationDatabase"];
            string DTS_DestinationTable = ConfigurationSettings.AppSettings["DTS_DestinationTable"];
            string DTS_DestinationUserName = ConfigurationSettings.AppSettings["DTS_DestinationUserName"];
            string DTS_DestinationPassword = ConfigurationSettings.AppSettings["DTS_DestinationPassword"];

            StringBuilder DTSArgs = new StringBuilder();

            DTSArgs.AppendFormat("/N \"{0}\" /G \"{1}\" /F \"{2}\" ",
                                        DTS_PackageName,
                                        DTS_PackageGUID,
                                        DTS_PackagePathAndFileName);            
            DTSArgs.AppendFormat(@"/A ""DestinationDatabase"":""8""=""{0}"" ", DTS_DestinationDatabase);
            DTSArgs.AppendFormat("/A \"DestinationPassword\":\"8\"=\"{0}\" ", DTS_DestinationPassword);
            DTSArgs.AppendFormat("/A \"DestinationServerName\":\"8\"=\"{0}\" ", DTS_DestinationServerName);
            DTSArgs.AppendFormat("/A \"DestinationTable\":\"8\"=\"{0}\" ", DTS_DestinationTable);
            DTSArgs.AppendFormat("/A \"DestinationUserName\":\"8\"=\"{0}\" ", DTS_DestinationUserName);
            DTSArgs.AppendFormat("/A \"ImportFileConnection\":\"8\"=\"{0}\" ", DTS_ImportFileConnection);
            DTSArgs.AppendFormat("/A \"ImportFileName\":\"8\"=\"{0}\" ", DTS_ImportFileName);
            DTSArgs.AppendFormat("/A \"ImportSourcePath\":\"8\"=\"{0}\" ", DTS_ImportSourcePath);
            DTSArgs.AppendFormat("/A \"ImportWorkingPath\":\"8\"=\"{0}\" ", DTS_ImportWorkingPath);
            DTSArgs.AppendFormat("/A \"ImportWorkingPathAndFileName\":\"8\"=\"{0}\" ", DTS_ImportWorkingPathAndFileName);
            DTSArgs.AppendFormat("/W \"0\"");

            System.Diagnostics.Process proc = new System.Diagnostics.Process();
            proc.EnableRaisingEvents = false;
            proc.StartInfo.FileName = "dtsrun ";
//**** PROBLEM IS HERE:  The DTSArgs escpaes the double-quotes and backslashes in paths with backslashes,
//**** then the dtsrun.exe doesn’t like the arguments (see string below)
            proc.StartInfo.Arguments = DTSArgs.ToString();

            proc.Start();

            proc.WaitForExit();

DTSARgs的样子:

  

/ N“BlockImportNEW”/ G.   “{C22A80D9-7613-43AF-939C-3C04AD7D848A}”   /F   “\计算机名\文件夹\文件夹1 \文件夹2 \ folder3 \ filename.dts”   /“DestinationDatabase”:“8”=“DBNAME”   /一个   “DestinationPassword”: “8”= “密码”   /一个   “DestinationServerName”: “8”= “服务器名”   /一个   “destinationTable会”: “8”= “dbname.dbo.tablename”   /一个   “DestinationUserName”: “8”= “username” 的   /一个   “ImportFileConnection”: “8”= “file.txt的 ';' ';' 0' ”   /“ImportFileName”:“8”=“file.txt”/ A.   “ImportSourcePath”: “8”= “C:\ BlockImport”   /一个   “ImportWorkingPath”: “8”= “\计算机名\文件夹\文件夹1 \文件夹2”   /一个   “ImportWorkingPathAndFileName”: “8”= “\计算机名\文件夹\文件夹1 \文件夹2 \ file.txt的”   / W“0”

proc.StartInfo.Arguments的内容如下:

  

/ N \“BlockImportNEW \”/ G.   \ “{C22A80D9-7613-43AF-939C-3C04AD7D848A} \”   /F   \ “\\计算机名\文件夹\文件夹1 \文件夹2 \ folder3 \ filename.dts \”   /一个   \ “DestinationDatabase \”:\ “8 \”= \ “DBNAME \”   /一个   \ “DestinationPassword \”:\ “8 \”= \ “密码\”   /一个   \ “DestinationServerName \”:\ “8 \”= \ “服务器名\”   /一个   \ “destinationTable会\”:\ “8 \”= \ “dbname.dbo.tablename \”   /一个   \ “DestinationUserName \”:\ “8 \”= \ “用户名\”   /一个   \ “ImportFileConnection \”:\ “8 \”= \ “file.txt的 ';' ';' 0' \”   /一个   \ “ImportFileName \”:\ “8 \”= \ “file.txt的\”   /一个   \ “ImportSourcePath \”:\ “8 \”= \ “C:\ BlockImport \”   /一个   \ “ImportWorkingPath \”:\ “8 \”= \ “\\计算机\文件夹\文件夹1 \文件夹2 \”   /一个   \ “ImportWorkingPathAndFileName \”:\ “8 \”= \ “\\计算机\文件夹\文件夹1 \文件夹2 \ file.txt的\”   / W \“0 \”

执行proc.Start时,由于所有额外的反斜杠,它无法正常工作。 似乎DTSArgs stringbuilder字符串格式正确,但是当通过.ToString()转换并存储在proc.StartInfo.Arguments中时,它会获得所有反斜杠。

如何避免发送proc.StartInfo.Arguments所有这些额外的反斜杠?

3 个答案:

答案 0 :(得分:4)

我强烈怀疑他们不是真的在弦乐中。您是否有机会使用调试器?它显示了字符串的转义形式。

将字符串写入控制台或类似的东西,你会看到真正的价值。

答案 1 :(得分:0)

您使用的是verbatim string literals吗?

即:

String c = @"C \B";

答案 2 :(得分:0)

我确实发现它只在调试器和即时窗口中显示转义字符。当写入Process时,它实际上已经过格式化校正。我创建了一个简单的批处理文件:

echo %1
pause

然后我用Process调用批处理文件而不是我试图运行的exe。 这样我就能看到实际发送的内容。之前我的部分问题是cmd窗口关闭得太快了。

感谢帮助人员!