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)
cannot convert from 'string' to 'System.IO.Stream' (At the same line)
'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)
As you can see I have SYstem.io reference
Any ideas where im going wrong?
Any help appreciated
Mark
Use 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);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.