a419443942 2017-06-05 16:46 采纳率: 50%
浏览 811
已结题

怎么将输入内容修改为保存到本地

现在我这代码是输入内容用Codesoft打印出来,现在想修改为将输入内容保存为.txt格式到本地或网络盘,如txtTargetQty为4则每输入4个保存为一个文件,并且名称以第一个输入内容命名
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.IO;
using System.Data.OleDb;
using System.Diagnostics;
using System.Web;
using System.Net;
using System.Collections;

namespace PrintSN
{
public partial class MainForm : Form
{
private DataTable myDs = new DataTable();

    public MainForm()
    {
        InitializeComponent();
    }

    private void txtInput_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar == (char)Keys.Enter)
        {
            string strInput = txtInput.Text;
            txtTargetQty.Text = Common.getConfigValueKey("PrintSNCount");
            if (strInput.Equals("UNDO"))
            {
                undo();
                txtInput.SelectAll();
                txtInput.Focus();
                return;
            }
            else
            {
                if (dataGridView1.Rows.Count >0)
                {
                    string strSN = "";
                    for (int i = 0; i < dataGridView1.Rows.Count; i++)
                    {
                        strSN = dataGridView1.Rows[i].Cells[0].Value.ToString();
                        if (strInput.Equals(strSN))
                        {
                            ShowInformation("SN 重复,请扫描其他的SN ! ", "NG");
                            txtInput.SelectAll();
                            txtInput.Focus();
                            return;
                        }
                    }
                }
                if (txtScanQty.Text.Equals((Int32.Parse(txtTargetQty.Text) - 1).ToString()))
                {
                    int index = this.dataGridView1.Rows.Add();
                    this.dataGridView1.Rows[index].Cells[0].Value = strInput;
                    ArrayList SnDetail = new ArrayList(); 
                    for (int i = 0; i < dataGridView1.Rows.Count; i++)
                    {  
                        SnDetail.Add(dataGridView1.Rows[i].Cells[0].Value.ToString());
                    }  
                    SoftCodePrint Print = new SoftCodePrint();
                    Print.PrintSNLabel(strInput, SnDetail);
                    txtScanQty.Text = "0";
                    dataGridView1.Rows.Clear();
                    ShowInformation("打印完成,请扫描的下一個SN ! ", "OK");
                }
                else
                {
                    txtScanQty.Text = (Int32.Parse(txtScanQty.Text) + 1).ToString();
                    ShowInformation("请扫描的下一個SN ! ", "OK");
                    int index = this.dataGridView1.Rows.Add();
                    this.dataGridView1.Rows[index].Cells[0].Value = strInput;


                }

                txtInput.SelectAll();
                txtInput.Focus();
                return;
            }

        }

    }

    private void undo()
    {
        //txtTargetQty.Text = "";
        txtScanQty.Text = "0";
        dataGridView1.Rows.Clear();
        ShowInformation("UNDO 操作成功,請掃描 SN! ", "OK");
    }

    private void ShowInformation(string sInfo, string sType)
    {
        lblShowMessageInfo.Text = sInfo;

        if (sType.Equals(""))
        {
            lblShowMessageInfo.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(128)), ((System.Byte)(128)), ((System.Byte)(255)));
        }
        if (sType.Equals("OK"))
        {
            lblShowMessageInfo.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(128)), ((System.Byte)(128)), ((System.Byte)(255)));
        }
        if (sType.Equals("NG"))
        {
            lblShowMessageInfo.BackColor = System.Drawing.Color.Red;
        }

    }

    private void MainForm_Load(object sender, EventArgs e)
    {
        txtTargetQty.Text = Common.getConfigValueKey("PrintSNCount");
    }

    private void MainForm_Shown(object sender, EventArgs e)
    {
        txtInput.SelectAll();
        txtInput.Focus();
    }

}

}


  • 写回答

1条回答 默认 最新

  • threenewbee 2017-06-05 17:09
    关注

    System.IO.File.WriteAllText(文件名, 你要保存的字符串);

    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?