2010-05-06 76 views
5

我有一個應用程序,我正在建設需要修改配置文件。C#File.ReadAllLines不會打破換行

我的問題是我無法逐行閱讀文件。我將整個文件作爲單個字符串進行保存。

string ConfigTemplate = AEBuildsSPFolderName + "\\Template_BuildReleaseScript.Config"; 

string[] fileSourceLines = File.ReadAllLines(ConfigTemplate, Encoding.Default); 
//Returns the entire file contents into the first array element. 

using (StreamReader reader = new StreamReader(ConfigTemplate)) 
      { 
       string line; 
       while ((line = reader.ReadLine()) != null) 
//Returns the entire file contents into the first line read. 

任何想法我做錯了什麼?

感謝,

大衛

回答

5

我猜使用可能不是\r\n

當你讀你的整個文件合併成一個字符串換行字符,嘗試調用yourString.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);,看看是否適合你。

此外,由於ReadAllLines()只是讀入單個字符串,您可以簡單地使用ReadAllText()

+0

感謝您的回覆。 基於您的意見,下面是測試代碼段我放在一起...... //獲取文件行 的String [] fileSourceLines = File.ReadAllLines(ConfigTemplate,Encoding.Default); (fileSourceLines.Length == 1) if(fileSourceLines.Length == 1) string yourString = fileSourceLines [0]; string [] newSourceLines = yourString.Split(new char [] {'\ r','\ n'},StringSplitOptions.RemoveEmptyEntries); //這個數組仍然只包含1​​個字符串。 } newSourceLines數組仍然只包含1​​個字符串。 任何其他建議:) 謝謝, 大衛 – 2010-05-06 01:59:45

+0

好吧,我才發現,這是這個問題.....感謝每個人的幫助! – 2010-05-06 02:03:28

0

您的文件可能使用\n字符(不\r)作爲換行符。