Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
2.00/5 (3 votes)
See more:
hi how can i download my file from server path on click button using c# windows application
my upload code is :
C#
DialogResult result = openFileDialog1.ShowDialog();
            if (result == DialogResult.OK) // Test result.
            {
                int count = 0;
                string[] FilenameName;
                foreach (string item in openFileDialog1.FileNames)
                {
                    FilenameName = item.Split('\\');
                    File.Copy(item, @"C:\Users\samer\Desktop\Files\" + FilenameName[FilenameName.Length - 1]);
                    count++;
                }
            }



please help :)

What I have tried:

FileStream FS = null;

string filepath = @"C:\Users\samer\Desktop\Files\image100.png";
FS = new FileStream(filepath, System.IO.FileMode.Create);


FS.Close();


it doesnt work ????
Posted
Updated 27-May-16 23:23pm
Comments
Sergey Alexandrovich Kryukov 27-May-16 18:50pm    
If this is the standard Windows file sharing service, the "downloading" is reduced to normal file copy. You are not trying to do anything else. What's the problem?

Don't use Split and string concatenation (+), instead, use System.IO.Path.
There are no situations when using hard-coded file paths can be useful.

—SA
ZurdoDev 27-May-16 22:33pm    
File.Copy will work if permissions and access are properly setup. So, what is the problem?

You should clean up your code a bit and use proper methods as mentioned in the comments.
C#
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK) // Test result.
{
    string pathDestination = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "Files");
    foreach (string item in openFileDialog1.FileNames)
    {
        File.Copy(item, Path.Combine(pathDestination, Path.GetFileName(item)));
    }
}

This code should work if you have the proper access rights.
 
Share this answer
 
Comments
sam9787 28-May-16 5:21am    
but how can i download this file again on click button ?
George Jonsson 28-May-16 5:34am    
How do you mean?
You have used file.copy function in your case, (you can call it as download file but actually its a copy activity) but if the server directory (source directory) is SHARED then only it works for you but if your source directory is not shared then you need use WebClient.DownloadFile Method(), (which Downloads the resource with the specified URI to a local file)
see below link for more details
WebClient.DownloadFile Method (String, String) (System.Net)[^]
 
Share this answer
 
Comments
sam9787 28-May-16 5:25am    
is this code work in windows application ?
koolprasad2003 28-May-16 5:28am    
Yes, it is for windows applications only, for web based application download file is simple. you want for windows or web ?
sam9787 28-May-16 5:29am    
i need windows :)
koolprasad2003 28-May-16 5:32am    
Yes it will working for Windows form only. (When there is no shared folder available on server)
sam9787 28-May-16 5:43am    
ok it work fine :)
but how can i allow the user to choose the file location to save ?
string remoteUri = @"C:\Users\samer\Desktop\Files\";
string fileName = "i.png", myStringWebResource = null;
// Create a new WebClient instance.
WebClient myWebClient = new WebClient();
// Concatenate the domain with the Web resource filename.
myStringWebResource = remoteUri + fileName;
Console.WriteLine("Downloading File \"{0}\" from \"{1}\" .......\n\n", fileName, myStringWebResource);
// Download the Web resource and save it into the current filesystem folder.
myWebClient.DownloadFile(myStringWebResource, fileName);
Console.WriteLine("Successfully Downloaded File \"{0}\" from \"{1}\"", fileName, myStringWebResource);
Console.WriteLine("\nDownloaded file saved in the following file system folder:\n\t" + Application.StartupPath);


this code save image in startuppath ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900