从文本文件读取不起作用,仅删除部分工作

时间:2016-08-01 04:50:02

标签: c#

我在阅读和删除txt文件时遇到问题。它根本不会读取,当我在列表框中删除某些内容时,它只删除程序中的信息,而不是文本文件本身。

我得到的错误是

  

错误CS7036没有给出对应的参数   需要形式参数'index'的'StreamReader.Read(char [],int,   INT)

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

namespace TripCostCalculator
{
public partial class Form1 : Form
{
    private const string dir = @"C:\C# 2015\Files\";
    private const string path = dir + "PayCheckCalculator.txt";
    decimal tax = .06m;

    public Form1()
    {
        InitializeComponent();
    }
    private void Form1_Load(object sender, EventArgs e)
    {
        if(!Directory.Exists(dir))
        Directory.CreateDirectory(dir);
        StreamReader textIn =
         new StreamReader(
         new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read));

        textIn.Read(txtName.Text);
        textIn.Read(txtDph.Text);
        textIn.Read(txtHoursWorked.Text);
        textIn.Read(txtGrossIncome);
        textIn.Read(txtNetIncome.Text);

        textIn.Close();
    }
    private void btnCalculate_Click(object sender, EventArgs e)
    {
        string name = txtName.Text;
        decimal dph;
        decimal grossIncome;
        decimal netIncome;

        if (!decimal.TryParse(txtDph.Text, out dph))
        {
            MessageBox.Show("Please enter a valid dollar amount");
            return;
        }
        decimal hoursWorked;
        if (!decimal.TryParse(txtHoursWorked.Text, out hoursWorked))
        {
            MessageBox.Show("Please enter a valid dollar amount");
            return;
        }
        grossIncome = dph * hoursWorked;
        txtGrossIncome.Text = grossIncome.ToString("c");
        netIncome = grossIncome - (grossIncome * tax);
        txtNetIncome.Text = netIncome.ToString("c");


    }

    private void btnDelete_Click(object sender, EventArgs e)
    {
        for (int v = 0; v < lstSalary.SelectedItems.Count; v++)
        {
            lstSalary.Items.Remove(lstSalary.SelectedItems[v]);
        }
        if (System.IO.File.Exists(@"C:\C# 2015\Files\"))
        {
            System.IO.File.Delete("PayCheckCalculator.txt");
        }

    }


    private void btnExit_Click(object sender, EventArgs e)
    {
        this.Close();
    }

    private void btnAdd_Click(object sender, EventArgs e)
    {

        lstSalary.Items.Add(txtName.Text);
        lstSalary.Items.Add(txtDph.Text);
        lstSalary.Items.Add(txtHoursWorked.Text);
        lstSalary.Items.Add(txtGrossIncome.Text);
        lstSalary.Items.Add(txtNetIncome.Text);

    }

    private void btnSave_Click_1(object sender, EventArgs e)
    {
        StreamWriter textOut = new StreamWriter(
         new FileStream(path, FileMode.OpenOrCreate | FileMode.Append, FileAccess.Write));



        textOut.Write(txtName.Text + ",");
        textOut.Write(txtDph.Text + ",");
        textOut.Write(txtHoursWorked.Text + ",");
        textOut.Write(txtGrossIncome + ",");
        textOut.Write(txtNetIncome.Text + ",");

        textOut.Close();

        txtName.Clear();
        txtDph.Clear();
        txtHoursWorked.Clear();
        txtGrossIncome.Clear();
        txtNetIncome.Clear();

}


}

}

1 个答案:

答案 0 :(得分:2)

要删除,您必须正确设置文件路径

if (System.IO.File.Exists(@"C:\C# 2015\Files\PayCheckCalculator.txt"))
   {
      System.IO.File.Delete(@"C:\C# 2015\Files\PayCheckCalculator.txt");
   }