I want to write a program where I can load files into, for example XML.
When the file is uploaded, I want to use the data that is in the file.
can someone help me please.


6 Years
Discussion Span
Last Post by superjj

There is no problem with showing the files (list of files), the problem is when you want to show file`s content. There is a big difference if file is xml, txt or some oteher type.
So what can it be?


I hope xlm, txt,... .But if I can open a txt then i'm happy.
When that works, I can change the program myself.


Ok, I did an example code, how to upload files and show then in the list (listBox control). Then I added a richTextBox control on the form which is meant to show the file`s content (text usually).

NOTE: Dont forget to add a new namespace on top of the class when using FileInfo and StreamReader classes. This namespace its called System.IO;
So here it is:

//add a namespace on top here:
using  System.IO;
//among other namespases...

    public partial class Form1 : Form
        List<FileInfo> listFiles;
        public Form1()

        private void button1_Click(object sender, EventArgs e)
            OpenFileDialog open = new OpenFileDialog();
            open.Filter = "text files (*.txt)|*.txt|xml files (*.xml)|*.xml";
            open.Multiselect = true;
            open.InitialDirectory = @"C:\";
            if (open.ShowDialog() == DialogResult.OK)
                listFiles = new List<FileInfo>();
                FileInfo fi;
                for (int i = 0; i < open.FileNames.Length; i++)
                    fi = new FileInfo(open.FileNames[i]);

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
            if (listBox1.SelectedIndex > -1)
                richTextBox1.Text = "";
                string fileName = listBox1.SelectedItems[0].ToString();
                foreach (FileInfo fi in listFiles)
                    if (fileName == Path.GetFileName(fi.FullName))
                        using (StreamReader sr = new StreamReader(fi.FullName))
                            string text;
                            while ((text = sr.ReadLine()) != null)
                                richTextBox1.AppendText(text + "\n");

This code works, and so far it only opends *.txt files.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.