[英]c# windows app writing text to file
Having real problems with writing to a text file using following code 使用以下代码在写入文本文件时遇到实际问题
using HCP1.Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.UI.Xaml.Media.Imaging;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.Storage;
using System.Threading.Tasks;
using System.Text;
using System.Diagnostics;
private void imageview_DoubleTapped(object sender, DoubleTappedRoutedEventArgs e)
{
var point = e.GetPosition(imageview);
var p1 = (int)point.X;
var p2 = (int)point.Y;
string stringVal1;
string stringVal2;
stringVal1 = System.Convert.ToString(p1);
stringVal2 = System.Convert.ToString(p2);
Text3.Text = stringVal1;
Text4.Text = stringVal2;
if (p2 > 210 && p2 < 235 && p1 > 339 && p1 < 367) {
string dave = Environment.NewLine + Text3.Text + "," + Text4.Text;
string path = @"C:\myfile.txt";
StreamWriter sw = new StreamWriter(path);
string bufferOne = dave;
sw.Write(bufferOne);
sw.Close();
sw.Dispose();
};
}
When a double tap is done it should save the contents of a textblock to a file but I have three errors 双击完成后,应将文本块的内容保存到文件中,但出现三个错误
The best overloaded method match for 'System.IO.StreamWriter.StreamWriter(System.IO.Stream)' has some invalid arguments (This is at the Streamwriter Line) 最佳重载方法匹配'System.IO.StreamWriter.StreamWriter(System.IO.Stream)'有一些无效的参数(这在Streamwriter行)
cannot convert from 'string' to 'System.IO.Stream' (At the same line) 无法从“字符串”转换为“ System.IO.Stream”(在同一行)
'System.IO.StreamWriter' does not contain a definition for 'Close' and no extension method 'Close' accepting a first argument of type 'System.IO.StreamWriter' could be found (are you missing a using directive or an assembly reference?) (This is at the Close line) 'System.IO.StreamWriter'不包含'Close'的定义,并且找不到扩展方法'Close'接受类型为'System.IO.StreamWriter'的第一个参数(您是否缺少using指令或程序集引用?)(在结束行)
As you can see I have SYstem.io reference 如您所见,我有SYstem.io参考
Any ideas where im going wrong? 任何想法,我错了吗?
Any help appreciated 任何帮助表示赞赏
Mark 标记
Use File.WriteAllText . 使用File.WriteAllText 。 It's a static method and does the same as opening, writing and closing a file.
这是一个静态方法,与打开,写入和关闭文件相同。 Remember, the less code the less bugs.
请记住,代码越少,错误越少。
if (p2 > 210 && p2 < 235 && p1 > 339 && p1 < 367)
{
string dave = Environment.NewLine + Text3.Text + "," + Text4.Text;
File.WriteAllText(@"C:\myfile.txt", dave);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.