簡體   English   中英

C#Windows應用程序將文本寫入文件

[英]c# windows app writing text to file

使用以下代碼在寫入文本文件時遇到實際問題

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();
            };
                }

雙擊完成后,應將文本塊的內容保存到文件中,但出現三個錯誤

最佳重載方法匹配'System.IO.StreamWriter.StreamWriter(System.IO.Stream)'有一些無效的參數(這在Streamwriter行)

無法從“字符串”轉換為“ System.IO.Stream”(在同一行)

'System.IO.StreamWriter'不包含'Close'的定義,並且找不到擴展方法'Close'接受類型為'System.IO.StreamWriter'的第一個參數(您是否缺少using指令或程序集引用?)(在結束行)

如您所見,我有SYstem.io參考

任何想法,我錯了嗎?

任何幫助表示贊賞

標記

使用File.WriteAllText 這是一個靜態方法,與打開,寫入和關閉文件相同。 請記住,代碼越少,錯誤越少。

  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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM