2009-01-01 62 views
1

我收到一個「FormatException:輸入字符串格式不正確」的錯誤,我不明白。將字符串寫入文件會導致C#中的異常

我用下面的幾行寫一個字符串到一個文本文件:

using (StreamWriter sw = new StreamWriter(myfilename, false, System.Text.Encoding.GetEncoding(enc))) 
{ 
    sw.Write(mystring, Environment.NewLine); 
} 

(編碼部分是因爲我有我的應用程序的選項,將其設置爲UTF-8或ISO -8859-1 ......但我認爲這是無關緊要的)。

我的所有字符串都寫得很好,除了這個字符串與其他字符串不同之外,因爲它實際上有一段JavaScript代碼。我確定那裏有一個特殊角色可能會導致問題,但我怎麼知道?

有一兩件事我想只是上面的sw.Write語句之前插入以下行:

System.Console.WriteLine(mystring); 

,並寫了控制檯就好了 - 沒有錯誤。

幫助?

謝謝! (新年快樂!)

-Adeena

回答

11

您正在使用的重載接受的格式作爲第一個參數,和對象後注入。

您可以執行下列操作:

sw.Write(mystring + Environment.NewLine); 

sw.Write("{0}{1}", mystring, Environment.NewLine); 
+0

工作,謝謝! (帶「+」的第一個語句) – adeena 2009-01-01 14:33:57

0

在回答DK的意見,我測試了一下擴展字符串連接較慢。我用三個選項做了這個設置;

  • 串聯串
  • 調用sw.Write兩次
  • 調用sw.WriteLine

在我的機器,第二個選項是高於平均水平快了大約88%。在10000000次迭代中,它們使用3517,2420和3385毫秒。

如果這是在程序中多次調用的代碼,它應該是重要的。

using System; 
using System.IO; 
using System.Text; 

class Program 
    { 
     static void Main(string[] args) 
     { 
      const string myString = "kdhlkhldhcøehdhkjehdkhekdhk"; 
      int iterations=getIntFromParams(args, 0, 10); 
      int method = getIntFromParams(args, 1, 0); 
      var fileName=Path.GetTempFileName(); 
      using (StreamWriter sw = new StreamWriter(fileName, false, Encoding.Default)) 
      { 
       switch (method) 
       { 
        case 0: 

         Console.WriteLine("Starting method with concatenation. Iterations: " + iterations); 
         var start0 = DateTimeOffset.Now; 
         for (int i = 0; i < iterations; i++) 
         { 
          sw.Write(myString + Environment.NewLine); 
         } 
         var time0 = DateTimeOffset.Now - start0; 
         Console.WriteLine("End at " + time0.TotalMilliseconds + " ms."); 

         break; 
        case 1: 
         Console.WriteLine("Starting method without concatenation. Iterations: " + iterations); 
         var start1 = DateTimeOffset.Now; 
         for (int i = 0; i < iterations; i++) 
         { 
          sw.Write(myString); 
          sw.Write(Environment.NewLine); 
         } 
         var time1 = DateTimeOffset.Now - start1; 
         Console.WriteLine("End at " + time1.TotalMilliseconds + " ms."); 
         break; 
        case 2: 
         Console.WriteLine("Starting method without concatenation, using WriteLine. Iterations: " + iterations); 
         var start2 = DateTimeOffset.Now; 
         for (int i = 0; i < iterations; i++) 
         { 
          sw.WriteLine(myString); 
         } 
         var time2 = DateTimeOffset.Now - start2; 
         Console.WriteLine("End at " + time2.TotalMilliseconds + " ms."); 
         break; 
       } 
      } 
     } 

     private static int getIntFromParams(string[] args, int index, int @default) 
     { 
      int value; 
      try 
      { 
       if (!int.TryParse(args[index], out value)) 
       { 
        value = @default; 
       } 
      } 
      catch(IndexOutOfRangeException) 
      { 
       value = @default; 
      } 
      return value; 
     } 
    }