hi

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.

Thanks

Recommended Answers

All 8 Replies

A win form?

yes

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()
        {
            InitializeComponent();
          
        }


        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]);
                    listFiles.Add(fi);
                    listBox1.Items.Add(Path.GetFileName(fi.FullName));
                }
            }
        }

        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.

nice 1
It work very well.

thank you

You can adda rep +1 :)
I was trying really hard on the Sunday afternoon.

of course.
I had forgotten to do that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.