2014-10-17 119 views
-4

的代碼是:需要幫助,閱讀一些C#

private static void loadAccounts() 
{ 
    using (TextReader tr = new StreamReader("accounts.txt")) 
    { 
     string line = null; 
     while ((line = tr.ReadLine()) != null) 
     { 
     String[] details = line.Split('\t'); 
     accounts.Add(details[0].Substring(6) + 
         ":" + details[1].Substring(10)); 
     } 
    } 
} 

應該怎樣文本文件包含/應如何contense被格式化。 我已經試過: 用戶名:密碼 用戶名/噸密碼

,但得到的錯誤

的錯誤是:

System.ArgumentOutOfRangeException:的startIndex不能大於字符串的長度。 在System.String.InternalSubStringWithChecks(的Int32的startIndex,的Int32長度,布爾fAlwaysCopy) 在System.String.Substring(的Int32的startIndex,的Int32長度) 在System.String.Substring(的Int32的startIndex) 在Test.Program.loadAccounts( ) c:\ Users \ Documents \ SharpDevelopProjects \ Test \ Test \ Program.cs:line 148 at Test.Program.Main(String [] args)in c:\ Users \ Documents \ SharpDevelopProjects \ Test \ Test \ Program.cs:line 26

+5

'\ t'是一個製表符。即Tab鍵 – 2014-10-17 06:43:36

+2

您的問題標題沒有太多說明。請參考[我如何寫一個好的標題?](http://meta.stackexchange.com/questions/10647/how-do-i-write-a-good-title)。 – 2014-10-17 06:44:11

+0

你得到了什麼錯誤? – Catwood 2014-10-17 06:44:59

回答

0

奇怪的代碼。

此行將文本文件中的一行分割爲製表符,例如, 「用戶名(這裏想象一個標籤)密碼」

String[] details = line.Split('\t'); 

此行丟棄該用戶名元件的前6個字符(信息[0]),增加了結腸,丟棄該密碼元素的前10個字符(細節[1])並將其連接成單個字符串。

accounts.Add(details[0].Substring(6) + ":" + details[1].Substring(10)); 
0

在文本文件中的所有行應該有: 最小用戶名長度= 6 最小密碼長度= 10 我也喜歡的東西「」或「替代「\ t」 - 」

  • MyNameIs-MyPasswordIsTheFollowing1
  • MyNameIs-MyPasswordIsTheFollowing2
  • MyNameIs-MyPasswordIsTheFollowing3
  • MyNameIs-MyPasswordIsTheFollowing4
+0

哦,我看到^ _ ^似乎現在工作,我也改變他們兩個1,不知道它的工作有多好,但沒有錯誤,非常感謝:D – Jordan 2014-10-17 07:00:31

+0

- 實現我將不得不做一些編輯因爲它也使任何真正的目的 – Jordan 2014-10-17 07:02:16

+0

約旦如果這解決您的問題比標記爲答案:-) – 2014-10-17 08:33:19