2016-08-01 61 views
0

我在讀取和從txt文件中刪除時遇到問題。它根本不會讀取,當我在列表框中刪除某些內容時,它只會從程序中刪除信息,而不是文本文件本身。從文本文件中讀取不起作用,只刪除部分作品

我得到的錯誤是

錯誤CS7036沒有給定的參數對應於StreamReader.Read」的 所需形式參數 '索引'(字符[],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(); 

} 


} 

} 

回答

2

刪除已設置正確的文件路徑

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

@最高選民:他錯過了@;在投票前糾正錯誤 –

+0

謝謝@非幸運 – Mostafiz

+0

另外,在路徑中使用空格沒有問題,請從問題中刪除它,因爲它是誤導性的(另外,您還沒有解決主要錯誤,這是他有讀取時出現問題) – Jcl