2012-06-17 97 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

是的,我完全同意。感谢编辑。 –