C# .NET - while exporting data to csv file how to give name of file dynamically

Asked By vijaya saladi on 01-Jun-12 03:00 AM
Hai.,  

                  In my windows application foe exporting the data in data grid I want to prompt the user to give the name of the file as his/her wish.But present I implemented code to store the file with unique name and it also produce error also while I export another file to that path i.e., the path is already exists with another process.
I used this code to export the data:

 System.IO.StreamWriter streamWriter = new System.IO.StreamWriter("D:\\outputs.csv");
 string strHeader = dataGridView1.Columns[0].HeaderText;
            for (int i = 1; i < dataGridView1.Columns.Count; i++)
            {
                strHeader += "," + dataGridView1.Columns[i].HeaderText;
            }
            streamWriter.WriteLine(strHeader);
            for (int m = 0; m < dataGridView1.Rows.Count; m++)
            {
                string strRowValue = "";
                strRowValue += dataGridView1.Rows[m].Cells[0].Value;
                for (int n = 1; n < dataGridView1.Columns.Count; n++)
                {
                    strRowValue += "," + dataGridView1.Rows[m].Cells[n].Value;
                }
                streamWriter.WriteLine(strRowValue);
            }
 streamWriter.Close();

the code just save the data with that outputs name for only once.But I want to save different file with different names in different locations.
So,Please provide the solution to store the file with different names.

Thanks In Advance.,

Thank You.,
Vijaya.S

[)ia6l0 iii replied to vijaya saladi on 01-Jun-12 10:26 AM
You could use a jQuery popup , to ask the user a name for the file that we wants to download to. You can then pass it on to the server, and the server can then do a response.write using the passed filename. 

A sample script is provided below.  Do not forget to add jQuery references. 

Create a popup with a textbox. 
<div id="filenamePopup" title="Download as?">
  <p>
     <span>Please provide a file name</span>
     <input type="text" id="filename" />
  </p>
</div>


Add the popup code.
$('#filenamePopup').dialog({
     modal: true,
     buttons: {
          'Accept': function() {
                       var filename = $(this).find('#filename').val();
             //store the filename in a hidden variable and then use it.
                       $(this).dialog('close');
                    }
'Cancel': function() {
                       $(this).dialog('close');
                    },
});

Finally, open the popup on a button click. In this example, I am using a button named "ButtonDownloadFile" to open the popup.
$('#ButtonDownloadFile').click( function() {
   $('#filenamePopup').dialog('open');
});

However, in my humble opinion you should not do that. You should just throw the file as a response to the browser, and the user will be shown the Open/Save dialog from the browser. The user should and most probably would choose "save", and then provide a new name for the file. That makes more sense. 

This way, you would not have to deal with the "file already exists" errors for yourself. 

Hope this helped.