2012-06-17 100 views
1
<Window 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
x:Class="Traning.dynamicnotpadxaml" 
x:Name="Window" 
Title="dynamicnotpadxaml" 
Width="346" Height="303"> 

<Canvas x:Name="LayoutRoot" Margin="14,32,10,8"> 
    <Label x:Name="lbl_fname" Content="First Name" Height="34" Width="87" Canvas.Left="13" Canvas.Top="21"/> 
    <TextBox x:Name="txt_fname" Height="34" TextWrapping="Wrap" Text="Chandra" Width="93" Canvas.Left="203" Canvas.Top="21"/> 
    <Label x:Name="lbl_lname" Content="Last Name" Height="34" Width="87" Canvas.Left="13" Canvas.Top="59"/> 
    <TextBox x:Name="txt_lname" Height="34" TextWrapping="Wrap" Text="Sekaran" Width="93" Canvas.Left="203" Canvas.Top="59"/> 
    <Label x:Name="lbl_age" Content="Age" Width="87" Height="34" Canvas.Left="13" Canvas.Top="97"/> 
    <TextBox TextWrapping="Wrap" Text="22" Width="93" Height="34" Canvas.Left="203" Canvas.Top="97"/> 
    <Button Content="Save" Height="40" Canvas.Left="98" Canvas.Top="178" Width="112"/> 
</Canvas> 

如何動態生成文本文件(記事本文件)

是這樣的

enter image description here

輸出如果我點擊保存按鈕上面的詳細信息應保存爲文本文檔文件(.txt),如下圖.i必須將此文件保存在本地磁盤d:\ Mydetails \ notebook.txt中。我應該爲此做些什麼。

enter image description here

回答

2

請看這裏:http://www.csharp-station.com/HowTo/ReadWriteTextFile.aspx

你需要給一個名字到年齡文本框不過,因爲你在你的例子沒有名字。 您需要連接Save Buttons Click事件處理程序,並將以下代碼複製到其中(只需在設計視圖中雙擊並自動將您帶到事件處理程序方法)。

例子是:

 using (TextWriter tw = new StreamWriter(@"d:\Mydetails\notebook.txt")) 
     { 
      // write a line of text to the file, you need to access your TextBox Values here 
      tw.WriteLine("First Name : " + txt_fname.Text); 
      tw.WriteLine("Last Name : " + txt_lname.Text); 
      tw.WriteLine("Age : " + txt_age.Text); 
     } 
+1

你應該總是使用「使用」實現IDisposable的對象塊,然後你不需要手動關閉它們。查看更改後的代碼。 –

+0

是的,我完全同意。感謝編輯。 –