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.
In my previous article I have shown you how you can Bind
DataGridView In Windows Application Using C#, How
to Minimize an Application to the Taskbar Tray in C#.Net | System Tray
Application C#, Email
Validation in Windows Application C#.Net and VB.Net | Validating Email ID in
TextBox in C# .Net, Displaying
More than One Month in the Windows Forms MonthCalendar Control Using C#.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
Thank You.. code is working properly but i want to show this process in progress bar how much file uploaded in bytes
ReplyDeleteThinks
ReplyDeletethank u
ReplyDeleteThank you very much..
ReplyDeleteThanks for your valuable comment
Deletevery use full thank you
ReplyDeleteThanks for your comment
DeleteI am try this code this is useful
ReplyDeleteI want copied file path save in database.
please help me
Hi
DeletePut the below value in string variable.
string imageDBPath="Images\" + FilenameName(FilenameName.Length - 1);
Now save the above value into the DB
How to download this file?
ReplyDelete