Monday, 9 June 2014

File Upload in C#.Net Windows Application

6/09/2014 - By Pranav Singh 10

I have seen so many people are facing problem while uploading file in windows application.  So while development path of upload directory not found error id most common. In this article I will show you how you can upload a file in windows application using C#.Net or VB.Net.  


So for this article first we will create a new windows application and add a form. In this form we will add a textbox and two button controls. One button control to browse the file and other one is for uploading the file.

Now here is the layout for the form.



Now in this form add an openfiledialog control. This control we will use for open the file select window.


Now open the code file and add the below code.

C#.Net
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace DemoWindowsApplication
{
    public partial class FileUpload : Form
    {
        public FileUpload()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DialogResult result = openFileDialog1.ShowDialog();
            if (result == DialogResult.OK) // Test result.
            {
                textBox1.Text = openFileDialog1.FileName;
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            int count = 0;
            string[] FilenameName;
            foreach (string item in openFileDialog1.FileNames)
            {
                FilenameName = item.Split('\\');
                File.Copy(item, @"Images\" + FilenameName[FilenameName.Length - 1]);
                count++;
            }
            MessageBox.Show(Convert.ToString(count) + " File(s) copied");
        }
    }
}

VB.Net
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms
Imports System.IO

Namespace DemoWindowsApplication
    Partial Public Class FileUpload
        Inherits Form
        Public Sub New()
            InitializeComponent()
        End Sub

        Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
            Dim result As DialogResult = openFileDialog1.ShowDialog()
            If result = DialogResult.OK Then
                ' Test result.
                textBox1.Text = openFileDialog1.FileName
            End If

        End Sub

        Private Sub button2_Click(ByVal sender As Object, ByVal e As EventArgs)
            Dim count As Integer = 0
            Dim FilenameName As String()
            For Each item As String In openFileDialog1.FileNames
                FilenameName = item.Split("\"c)
                File.Copy(item, "Images\" + FilenameName(FilenameName.Length - 1))
                System.Math.Max(System.Threading.Interlocked.Increment(count), count - 1)
            Next
            MessageBox.Show(Convert.ToString(count) + " File(s) copied")
        End Sub
    End Class
End Namespace

In above code we have used below mention code for uploading the file

private void button2_Click(object sender, EventArgs e)
        {
            int count = 0;
            string[] FilenameName;
            foreach (string item in openFileDialog1.FileNames)
            {
                FilenameName = item.Split('\\');
                File.Copy(item, @"Images\" + FilenameName[FilenameName.Length - 1]);
                count++;
            }
            MessageBox.Show(Convert.ToString(count) + " File(s) copied");
        }

Now run the application by putting break point on upload button click event. Now here we will see that we are facing an error message that “Upload file path not found”.  Please check the below image.



This is the most common error faced by developer while developing the file upload in windows application.
Now we will check out bin directory. In this directory we are not having images folder.

 

So we will create image folder in our bind directory.



Now again run the application and try to upload the file. File will upload successfully.



Now again check the images directory in bin folder. You will get the uploaded file.





DOWNLOAD


About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

10 comments:

  1. Thank You.. code is working properly but i want to show this process in progress bar how much file uploaded in bytes

    ReplyDelete
  2. very use full thank you

    ReplyDelete
  3. I am try this code this is useful
    I want copied file path save in database.
    please help me

    ReplyDelete
    Replies
    1. Hi
      Put the below value in string variable.
      string imageDBPath="Images\" + FilenameName(FilenameName.Length - 1);

      Now save the above value into the DB

      Delete

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top