Click here to Skip to main content
15,921,716 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
i am working on windows application .How to download txt file using c# code on button click events

Thanks
phoolchand
Posted
Updated 13-Mar-12 22:57pm
v7

 
Share this answer
 
Comments
phoolchand yadav 14-Mar-12 4:54am    
this is not working
string remoteUri = "http://www.contoso.com/library/homepage/images/";
        string fileName = "ms-banner.gif", 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);
 
Share this answer
 
Comments
phoolchand yadav 14-Mar-12 7:01am    
Thanks you very much
 
Share this answer
 
Code to write csv file from dataTable

C#
public Boolean CreateOutputFile(DataTable dataTable)
       {
           try
           {
               List<String> csvFileColumns = new List<String>();
               csvFileColumns.Add("Column1");
               csvFileColumns.Add("Column2");
               using (StreamWriter streamWriter = new StreamWriter("C:\\data.csv"))
               {
                   // code to write CSV File Header
                   csvFileColumns.ForEach(delegate(String columnName)
                   {
                       streamWriter.Write(columnName);
                       streamWriter.Write(",");
                   });

                   streamWriter.Write(streamWriter.NewLine);
                   //fetch each row
                   if (dataTable.Rows.Count > 0)
                   {
                       foreach (DataRow dataRow in Data.Rows)
                       {

                           csvFileColumns.ForEach(delegate(String columnName)
                               {
                                   String value = String.Empty;
                                   if (value.Equals(String.Empty))
                                   {
                                       if (!dataRow.IsNull(columnName))
                                       {
                                           value = dataRow[columnName].ToString();
                                       }
                                   }
                                   streamWriter.Write(value);
                                   streamWriter.Write(",");
                               });
                           streamWriter.Write(streamWriter.NewLine);
                       }
                   }
               }
           }
           catch (IOException exp)
           {

           }
           return true;
       }
 
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