954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

uploading files and use them

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

superjj
Light Poster
32 posts since Nov 2010
Reputation Points: 10
Solved Threads: 1
 

A win form?

Mitja Bonca
Nearly a Posting Maven
2,485 posts since May 2009
Reputation Points: 641
Solved Threads: 474
 

yes

superjj
Light Poster
32 posts since Nov 2010
Reputation Points: 10
Solved Threads: 1
 

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?

Mitja Bonca
Nearly a Posting Maven
2,485 posts since May 2009
Reputation Points: 641
Solved Threads: 474
 

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

superjj
Light Poster
32 posts since Nov 2010
Reputation Points: 10
Solved Threads: 1
 

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.

Mitja Bonca
Nearly a Posting Maven
2,485 posts since May 2009
Reputation Points: 641
Solved Threads: 474
 

nice 1
It work very well.

thank you

superjj
Light Poster
32 posts since Nov 2010
Reputation Points: 10
Solved Threads: 1
 

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

Mitja Bonca
Nearly a Posting Maven
2,485 posts since May 2009
Reputation Points: 641
Solved Threads: 474
 

of course.
I had forgotten to do that.

superjj
Light Poster
32 posts since Nov 2010
Reputation Points: 10
Solved Threads: 1
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: