用C#写一个文本文件 [英] Write a text file in C#

查看:93
本文介绍了用C#写一个文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑到所有各种信息,这看起来应该非常容易。但是在写出我的文件时我总是抛出异常。我得到的错误是:



mscorlib.dll中发生了'System.UnauthorizedAccessException'类型的未处理异常



附加信息:拒绝访问路径'C:\ PayCheckCalculator.txt'



我尝试过:



This seems like it should be incredibly easy, given all of the various information out there. But I keep throwing an exception when writing out my files. The error I get is:

An unhandled exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll

Additional information: Access to the path 'C:\ PayCheckCalculator.txt' is denied

What I have tried:

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:\\ ";
        private const string path = dir + "PayCheckCalculator.txt";
        
        decimal tax = .06m;

        public Form1()
        {
            InitializeComponent();
        }

        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)
        {

        }

      
        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.Create, FileAccess.Write));
            
                textOut.Write(txtName.Text + "|");
                textOut.Write(txtDph.Text + "|");
                textOut.Write(txtHoursWorked.Text + "|");
                textOut.Write(txtGrossIncome + "|");
                textOut.Write(txtNetIncome.Text + "|");

            textOut.Close();
        }
    }
}

推荐答案

基本上它意味着你无权写入c:驱动器的根。使用您具有写入权限的目录。
Basically it means you do not have access to write to the root of the c: drive. Use a directory you have write access to.


这篇关于用C#写一个文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆