问题读取和写入在C#中的文本文件 [英] Problems reading and writing to a text file in C#

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

问题描述

好了,所以我已经看过,涉及到我的问题,不能看到我错了所有其他的问题。我的目标是写一个骰子模拟器到一个文本文件的结果,然后阅读内容到C#中的列表框。这里是我到目前为止的代码:

Ok so I have looked at every other question that relates to my problem and can't see where I am going wrong. My objective is to write the results of a dice simulator to a text file then read that contents into a listbox in C#. Here is my code so far:

namespace WpfApplication1
{
    public MainWindow()
    {
        InitializeComponent();

    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        try
        {
            Random RandomValue = new Random();
            Random RandomValue2 = new Random();

            using (StreamWriter outputFile = new StreamWriter("C:\\Example.txt"))
            {

                for (int i = 0; i < 100; i++)
                {

                    int face1 = RandomValue.Next(1, 7);
                    int face2 = RandomValue2.Next(1, 7);
                    int toss = face1 + face2;

                    outputFile.WriteLine(toss);

                }

                outputFile.Close();
            }

        }
        catch (Exception ex)
        {

            listBox1.Items.Add(ex);
        }

    }

    private void button2_Click(object sender, RoutedEventArgs e)
    {
        try
        {

            using (StreamReader inputFile = new StreamReader("C:\\Example.txt"))
            {

            String toss;
            for (int i = 0; i > 100; i++)
            {
                toss = inputFile.ReadLine();
                listBox1.Items.Add(toss);
            }

        catch (Exception ex)
        {
             listBox1.Items.Add(ex);
        }
       }
      }
    }
}

当我尝试运行程序我收到错误: System.UnauthorizedAccessException:对路径C:\Example.txt'。被拒绝

When I try running the program I receive the error: System.UnauthorizedAccessException: Access to the path 'C:\Example.txt' is denied.

我觉得我要去的地方错了其实就是创建文本文件。当我尝试从它读我收到了同样的信息。我非常新的C#和有我没有正确调用文件的感觉。任何有识之士将是非常欢迎!

I think that where I am going wrong is actually creating the text file. When I try to read from it I receive the same message. I am extremely new to C# and have the feeling that I am not calling the file correctly. Any insight would be extremely welcome!

推荐答案

好吧大家,首先我要感谢大家的帮助。我发现我要去哪里错了,为什么它不能正常工作。首先,运行C#作为管理员解决访问问题,谢谢Selman22。之后,它被写入文件就好了,但不会读取文件,移动数字到列表框中。更仔细地检查代码后,我意识到,我宣布,只要我是循环运行大于100,而不是更小的:

Alright everyone, first I want to thank you all for your help. I found where I was going wrong and why it was not working correctly. First, running C# as an administrator solved the access issues, thank you Selman22. After that it was writing to the file just fine but would not read the file and move the digits to the listbox. After reviewing the code more closely I realized I was declaring the for loop to run as long as I was greater than 100 rather than smaller:

的(INT I = 0; I> 100;我++)

改变了语句之后:

的for(int i = 0; I< 100;我++)

据现在运行平稳。谢谢大家了!

It now runs smoothly. Thank you all again!

这篇关于问题读取和写入在C#中的文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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