2009-12-08 53 views
1

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」 /A 「DestinationDatabase」: 「8」= 「DBNAME」 /甲 「DestinationPassword」: 「8」= 「密碼」 /A 「DestinationServerName」: 「8」= 「服務器名」 /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 「

看起來什麼proc.StartInfo.Arguments像:

/N \」 BlockImportNEW \」/G \ 「{C22A80D9-7613-43AF-939C-3C04AD7D848A} \」 /F \「\\ computername \ foldername \ folder1 \ folder2 \ folder3 \ filename.dts \」「目標數據庫」:\「8 \」= \「DBNAME \」 /A \「DestinationPassword \」:\「8 \」= \「password \」 /「:\」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 \ 「= \」 \\計算機\文件夾\ folder1中\文件夾2 \」 /A \ 「ImportWorkingPathAndFileName \」:\ 「8 \」= \ 「\\計算機\文件夾\文件夾1 \文件夾2 \ file.txt的\」 /W \ 「0 \」

當proc.Start被執行時,由於所有額外的反斜槓,它不能正常工作。 看來DTSArgs的stringbuilder字符串格式正確,但通過.ToString()轉換並存儲在proc.StartInfo.Arguments中時,它將獲得所有反斜槓。

如何防止發送proc.StartInfo.Arguments所有這些額外的反斜槓?

回答

4

我強烈懷疑他們不是真的在字符串中。你有沒有使用調試器?它顯示字符串的轉義形式。

將字符串寫入控制檯或類似的東西,你會看到真正的價值。

0

您使用的是verbatim string literals

即:

String c = @"C \B"; 
+0

你的意思是*逐字字符串文字*。只是「x y」是一個字符串文字。 – 2009-12-08 17:35:58

0

我沒發現它是隻用在調試器和直接窗口的轉義字符顯示。當寫入流程時,它實際上已被格式化了。我創建了一個簡單的做了這個批處理文件:

echo %1 
pause 

那時,我便與過程,而不是我試圖運行exe批處理文件。 這樣我就可以看到實際發送的內容。我之前的一部分問題是cmd窗口關閉得太快。

感謝您的幫助!

相關問題