Click here to Skip to main content
15,887,175 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an Excel file named "sample.xlx" in my Project Folder named "File Folder".I have a button named "btnDownload".When the user click this button the above excel file should be saved on local drive.The path of the file should be dynamic so that when the project is deployed on other PC then the path should not break.Note the application is offline that means the saving of file don't require internet connectivity.
How to solve this problem?

What I have tried:

private void btnDownload_Click(object sender, EventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter= "Excel file |*.xls;*.xlsx";
if(sfd.ShowDialog()==DialogResult.OK)
{
Stream fileStream = sfd.OpenFile();
StreamWriter sw = new StreamWriter(fileStream);
sw.Write("jhdfhsefefdhk");
sw.Close();
fileStream.Close();
}
}
Posted
Updated 12-Jan-20 3:19am
v2

You first need to read the source file in order to copy it to the destination. But also, you should use a binary stream for reading and writing as Excel files are not in simple text format.
 
Share this answer
 
Comments
Maciej Los 12-Jan-20 11:36am    
5ed!
You don't even need streams for that. There is a file class which supports file copy among others.

Here File.Copy Method (System.IO) | Microsoft Docs[^] you will find
public static void Copy (string sourceFileName, string destFileName);
 
Share this answer
 
Comments
Maciej Los 12-Jan-20 11:36am    
5ed!
[no name] 12-Jan-20 11:41am    
Thank you Maciej.

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