[WindowsPhone] 隨貼即用 - 取得專案中文字檔的資料
2013-10-16
簡述 : 從專案裡面建立一個文字檔(*.txt)取得裡面的內部資料
難度 : ★★
範例敘述: 在專案中,建立一個data.txt檔案裡面有一些中文文字,畫面上有一顆按鈕,按下後,將文字檔的內容用MessageBox 秀出來
程式碼:
一個method 專門呼叫,回傳為string 也就是檔案的內容,傳入值就是你要讀取的檔案路徑
/// <summary>/// 呼叫內建資源的文字檔案/// </summary>/// <param name="filename"></param>/// <returns></returns>public static string GetDataStoreFromResource(string filename)
{var resource = Application.GetResourceStream(new Uri(@"/" + MethodBase.GetCurrentMethod().DeclaringType.Namespace + ";component/" + filename, UriKind.Relative));
var streamReader = new StreamReader(resource.Stream);var x = streamReader.ReadToEnd();
streamReader.Close();
return x;}
呼叫程式範例
private void btnReadFileContent_Click(object sender, RoutedEventArgs e)
{ MessageBox.Show(GetDataStoreFromResource("data.txt"));}
特別注意: 必須要將加入專案的文字檔,Build Action 改成 Resource ,Copy Output Directory 改成Copy Always
下載源碼:
標籤:
隨貼即用
,
C#
,
WindowsPhone
-- Yesterday I wrote down the code. I bet I could be your hero. I am a mighty little programmer. 如果這篇文章有幫助到您,簡單留個言,或是幫我按個讚,讓我有寫下去的動力...
