簡體   English   中英

將數據保存到.txt文件中,同時單擊“讀取”按鈕

[英]save data to .txt file at the same time click “read” button

我想在單擊“讀取”按鈕時將接收到的數據保存到.txt文件。 請幫我

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO.Ports;

namespace Serial
{
    public partial class Form1 : Form
    {
        static SerialPort serialPort1;
        public Form1()
        {
            InitializeComponent();
            getAvailablePorts();  
            serialPort1 = new SerialPort();
        }
        void getAvailablePorts()
        {
            string[] Ports = SerialPort.GetPortNames();
            comboBox1.Items.AddRange(Ports);
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                if (comboBox1.Text == "" || comboBox2.Text == "")
                {
                    textBox2.Text = "Please select port Setting";
                }
                else
                {
                    serialPort1.PortName = comboBox1.Text;
                    serialPort1.BaudRate = Convert.ToInt32(comboBox2.Text);
                    serialPort1.Open();
                    progressBar1.Value = 100;
                    button1.Enabled = true;
                    button2.Enabled = true;
                    textBox1.Enabled = true;
                    button3.Enabled = false;
                    button4.Enabled = true;                    
                }
            }
            catch (UnauthorizedAccessException)
            {
                textBox2.Text = "anauthorized Acess";
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
                    serialPort1.Close();
                    progressBar1.Value = 0;
                    button1.Enabled = false;
                    button2.Enabled = false;
                    button4.Enabled = false; 
                    button3.Enabled = true;
                    textBox1.Enabled = false;           
        }

        private void button1_Click(object sender, EventArgs e)
        {
            serialPort1.WriteLine(textBox1.Text);
            textBox1.Text = "";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                textBox2.Text = serialPort1.ReadLine();

            }
            catch(TimeoutException)
            {
                textBox2.Text = "Timeout Exception";
            }
        }  

    }
}

只需使用File.WriteAllText

  File.WriteAllText("path here","text here")

只需使用StreamWriter

 using(StreamWriter sw = new StreamWriter("filename.txt")){
       sw.WriteLine(textbox.Text);
       sw.Close();
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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