Click here to Skip to main content
15,883,978 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all,

I have created a CSV file with data in Windows Application successfully.
On button click i need to download the CSV file in user defined location.

I have CSV file path path = "MyProjectpath../.csv";
How can i download this file on button click ?


In web application we use Response,
C#
Response.ClearContent();
           Response.AddHeader("content-disposition", "attachment;filename=Tickets.csv");
           Response.ContentType = "text/csv";

          ////
           Response.Write(sw.ToString());

           Response.End();


we cannot use Response in windows application .

How Can we do this Windows application in C# ?

Edit : I was Creating a setUp file(.exe). Customers can install setup in any folder. CSV file Exists in setup installed folder, i can't tell them copy CSV file from your installation path.
so, i am giving option to download
Posted
Updated 23-Jan-15 2:34am
v3
Comments
ZurdoDev 23-Jan-15 8:32am    
Can't you just use File.Copy? https://msdn.microsoft.com/en-us/library/system.io.file.copy(v=vs.110).aspx
dorababu407 23-Jan-15 8:39am    
File.Copy can only copy from source to destination file. it cannot download .
i want to open a Dialog to save file in user defined location.
ZurdoDev 23-Jan-15 8:41am    
Download is really a web concept. You just want to use the SaveAs dialog then. https://msdn.microsoft.com/en-us/library/sfezx97z%28v=vs.90%29.aspx
dorababu407 23-Jan-15 8:56am    
@RyanDev: thank you, i used SaveFileDialog it works for me
ZurdoDev 23-Jan-15 8:59am    
Glad to hear. I'll post as solution.

You'll want to use the SaveAs Dialog. This allows the user to chose where to save the file. See https://msdn.microsoft.com/en-us/library/sfezx97z%28v=vs.90%29.aspx[^]
 
Share this answer
 
SaveFileDialog saveFileDialog = new SaveFileDialog();
               saveFileDialog.Title = "Save as .csv File";
               saveFileDialog.Filter = "csv files (*.csv)|*.csv";
               saveFileDialog.FilterIndex = 2;

               saveFileDialog.FileName = "ExportData.csv";


               if (saveFileDialog.ShowDialog() == DialogResult.OK)
               {

                   if (saveFileDialog.FileName != "")
                   {

                       string filePath = saveFileDialog.FileName;

                       StreamWriter sw2 = new StreamWriter(filePath);

                       sw2.WriteLine("\"TicketId\",\"OrderDetailId\",\"IsTicketValidated\",\"ScanningTime\"");

                  // listObj is model list having Data
                       foreach (var line in listObj)
                       {
                           sw2.WriteLine(string.Format("\"{0}\",\"{1}\",\"{2}\",\"{3}\"",
                                                      line.TicketId,
                                                      line.OrderDetailId,
                                                      line.IsTicketValidated,
                                                      line.ScanningTime));
                       }

                       sw2.Close();

                   }
               }
 
Share this answer
 

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