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

发布于 2024-08-13 21:00:58 字数 6500 浏览 1 评论 0原文

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“块导入NEW”/G “{C22A80D9-7613-43AF-939C-3C04AD7D848A}” /F “\计算机名\文件夹名\文件夹1\文件夹2\文件夹3\文件名.dts” /A "DestinationDatabase":"8"="DBNAME" /一个 “目的地密码”:“8”=“密码” /一个 "目标服务器名称":"8"="服务器名称" /一个 "DestinationTable":"8"="dbname.dbo.tablename" /一个 "目标用户名":"8"="用户名" /一个 "ImportFileConnection":"8"="file.txt';'';'0'" /A "导入文件名":"8"="file.txt" /A "ImportSourcePath":"8"="C:\BlockImport" /一个 "ImportWorkingPath":"8"="\计算机名\文件夹名\文件夹1\文件夹2" /一个 "ImportWorkingPathAndFileName":"8"="\计算机名\文件夹名\folder1\folder2\file.txt" /W“0”

proc.StartInfo.Arguments 的样子:

/N \"阻止导入NEW\" /G \“{C22A80D9-7613-43AF-939C-3C04AD7D848A}\” /F \“\\计算机名\文件夹名\文件夹1\文件夹2\文件夹3\文件名.dts\” /一个 \"DestinationDatabase\":\"8\"=\"DBNAME\" /一个 \"目标密码\":\"8\"=\"密码\" /一个 \"目标服务器名称\":\"8\"=\"服务器名称\" /一个 \"DestinationTable\":\"8\"=\"dbname.dbo.tablename\" /一个 \"DestinationUserName\":\"8\"=\"userName\" /一个 \"ImportFileConnection\":\"8\"=\"file.txt';'';'0'\" /一个 \"ImportFileName\":\"8\"=\"file.txt\" /一个 \"ImportSourcePath\":\"8\"=\"C:\BlockImport\" /一个 \"ImportWorkingPath\":\"8\"=\"\\计算机名\文件夹名\folder1\folder2\" /一个 \"ImportWorkingPathAndFileName\":\"8\"=\"\\计算机名\文件夹名\folder1\folder2\file.txt\" /W“0”

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

我怎样才能避免发送 proc.StartInfo.Arguments 所有这些额外的反斜杠?

App.Config file contents:

    <?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>

Code:

            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();

What DTSARgs looks like:

/N "BlockImportNEW" /G
"{C22A80D9-7613-43AF-939C-3C04AD7D848A}"
/F
"\computername\foldername\folder1\folder2\folder3\filename.dts"
/A "DestinationDatabase":"8"="DBNAME"
/A
"DestinationPassword":"8"="password"
/A
"DestinationServerName":"8"="ServerName"
/A
"DestinationTable":"8"="dbname.dbo.tablename"
/A
"DestinationUserName":"8"="userName"
/A
"ImportFileConnection":"8"="file.txt';'';'0'"
/A "ImportFileName":"8"="file.txt" /A
"ImportSourcePath":"8"="C:\BlockImport"
/A
"ImportWorkingPath":"8"="\computername\foldername\folder1\folder2"
/A
"ImportWorkingPathAndFileName":"8"="\computername\foldername\folder1\folder2\file.txt"
/W "0"

What proc.StartInfo.Arguments looks like:

/N \"BlockImportNEW\" /G
\"{C22A80D9-7613-43AF-939C-3C04AD7D848A}\"
/F
\"\\computername\foldername\folder1\folder2\folder3\filename.dts\"
/A
\"DestinationDatabase\":\"8\"=\"DBNAME\"
/A
\"DestinationPassword\":\"8\"=\"password\"
/A
\"DestinationServerName\":\"8\"=\"ServerName\"
/A
\"DestinationTable\":\"8\"=\"dbname.dbo.tablename\"
/A
\"DestinationUserName\":\"8\"=\"userName\"
/A
\"ImportFileConnection\":\"8\"=\"file.txt';'';'0'\"
/A
\"ImportFileName\":\"8\"=\"file.txt\"
/A
\"ImportSourcePath\":\"8\"=\"C:\BlockImport\"
/A
\"ImportWorkingPath\":\"8\"=\"\\computername\foldername\folder1\folder2\"
/A
\"ImportWorkingPathAndFileName\":\"8\"=\"\\computername\foldername\folder1\folder2\file.txt\"
/W \"0\"

When proc.Start is executed, it doesn't work correctly because of all the extra backslashes.
It seems the DTSArgs stringbuilder string is in the correct format, but when converted via .ToString() and stored in proc.StartInfo.Arguments it gets all the backslashes.

How can I keep from sending proc.StartInfo.Arguments all those extra backslashes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

九歌凝 2024-08-20 21:00:58

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

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

I strongly suspect they aren't really in the string. Are you using the debugger by any chance? It shows an escaped form of the string.

Write the string to the console or something similar and you'll see the real value.

蹲墙角沉默 2024-08-20 21:00:58

您是否使用逐字字符串文字

IE :

String c = @"C \B";

Are you using verbatim string literals?

Ie :

String c = @"C \B";
小瓶盖 2024-08-20 21:00:58

我确实发现它仅在调试器和立即窗口中显示转义字符。当写出到进程时,它实际上被格式化了。我创建了一个批处理文件,它只是执行此操作:

echo %1
pause

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

感谢各位的帮助!

I did find that it was only showing with the escape characters in the debugger and immediate window. When written out to the Process, it was actually formatted corrected. I created a batch file that simply did this:

echo %1
pause

Then I called the batch file with the Process instead of the exe I was trying to run.
That way I could see what was actually being sent. Part of my problem before was that the cmd window closed too fast.

Thanks for the help guys!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文