获取文本的某些行c#

发布于 2024-08-22 14:56:54 字数 1062 浏览 5 评论 0原文

我想知道是否有人可以帮助我。我希望能够让我的程序使用户能够仅从文本文档中读取特定的代码块。不过,我希望将其放置在按钮后面,以便可以打开和关闭它。我尝试过不同的方法来做到这一点,但没有任何效果有丝毫不同。

这就是我的代码目前的情况。

namespace filestream
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private string Read(string file)
        {
            StreamReader reader = new StreamReader(file);
            string data = reader.ReadToEnd();
            reader.Close();

            return data;
        }

        private void btn1_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            DialogResult result = openFileDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                string data = Read(openFileDialog1.FileName);
                textBox1.Text = data;

            }
            else
            {
                //do nothing
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
        }
    }
}

我对此很陌生,因此我们将不胜感激。

I was wondering if someone could help me. I would like to able to be to have my program give the user the ability to only read a certain block of code from a text document. However I would like it to be placed behind a button so it can be turned on and off. I have experimented wih different ways of doing this but nothing has made the slightest bit of difference.

This is how my code stands at the moment.

namespace filestream
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private string Read(string file)
        {
            StreamReader reader = new StreamReader(file);
            string data = reader.ReadToEnd();
            reader.Close();

            return data;
        }

        private void btn1_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            DialogResult result = openFileDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                string data = Read(openFileDialog1.FileName);
                textBox1.Text = data;

            }
            else
            {
                //do nothing
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
        }
    }
}

Im quite new at this so any help would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

盗梦空间 2024-08-29 14:56:54

据我所知,从你那里的情况来看,它应该有效。我唯一能想到的原因是您的按钮连接到了button1_Click例程而不是btn1_Click例程。如果当您单击按钮时它没有执行任何操作,这是我能看到的唯一原因。该代码看起来像是要求用户选择一个文件,然后读取整个文件并将其放入文本框中。

As far as I can see from what you have there, it should be working. The only thing I can think of why it is not is that your button is connected to the button1_Click routine instead of the btn1_Click routine. If when you click the button and it does not do anything, that is the only reason I can see. The code looks like it is written to ask for the user to select a file and then read the whole file in and place it in the text box.

波浪屿的海角声 2024-08-29 14:56:54

如果您可以使用 .Net 3.5 和 LINQ,这里是一个选项......

public static class Tools
{
    public static IEnumerable<string> ReadAsLines(this string filename)
    {
        using (var reader = new StreamReader(filename))
            while (!reader.EndOfStream)
                yield return reader.ReadLine();
    }
}
class Program
{
    static void Main(string[] args)
    {
        var lines = "myfile.txt".ReadAsLines()
                                // you could even add a filter query/formatting
                                .Skip(100).Take(10) //do paging here
                                .ToArray();
    }
}

扩展疯狂以显示过滤、解析和格式化...

public static class Tools
{
    public static void Foreach<T>(this IEnumerable<T> input, Action<T> action)
    {
        foreach (var item in input)
            action(item);
    }
}
class Program
{
    static void Main(string[] args)
    {
        // the line below is standing in for your text file.  
        // this could be replaced by anything that returns IEnumerable<string>
        var data = new [] { "A 1 3", "B 2 5", "A 1 6", "G 2 7" };

        var format = "Alt: {1} BpM: {2} Type: {0}";

        var lines = from line in data
                    where line.StartsWith("A")
                    let parts = line.Split(' ')
                    let formatted = string.Format(format, parts)
                    select formatted;

        var page = lines.Skip(1).Take(2);

        page.Foreach(Console.WriteLine);
        // at this point the following will be written to the console
        //
        // Alt: 1 BpM: 6 Type: A
        //
    }
}

If you can use .Net 3.5 and LINQ here is an option...

public static class Tools
{
    public static IEnumerable<string> ReadAsLines(this string filename)
    {
        using (var reader = new StreamReader(filename))
            while (!reader.EndOfStream)
                yield return reader.ReadLine();
    }
}
class Program
{
    static void Main(string[] args)
    {
        var lines = "myfile.txt".ReadAsLines()
                                // you could even add a filter query/formatting
                                .Skip(100).Take(10) //do paging here
                                .ToArray();
    }
}

... extended insanity to show filtering, parsing, and formatting ...

public static class Tools
{
    public static void Foreach<T>(this IEnumerable<T> input, Action<T> action)
    {
        foreach (var item in input)
            action(item);
    }
}
class Program
{
    static void Main(string[] args)
    {
        // the line below is standing in for your text file.  
        // this could be replaced by anything that returns IEnumerable<string>
        var data = new [] { "A 1 3", "B 2 5", "A 1 6", "G 2 7" };

        var format = "Alt: {1} BpM: {2} Type: {0}";

        var lines = from line in data
                    where line.StartsWith("A")
                    let parts = line.Split(' ')
                    let formatted = string.Format(format, parts)
                    select formatted;

        var page = lines.Skip(1).Take(2);

        page.Foreach(Console.WriteLine);
        // at this point the following will be written to the console
        //
        // Alt: 1 BpM: 6 Type: A
        //
    }
}
櫻之舞 2024-08-29 14:56:54

您是否尝试过将 btn1_click 的内容放入 button1_click 中?有时它对我有用。

Have you tried taking the contents of btn1_click and putting it in button1_click? Sometimes it works for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文