ERROR - System.UnauthorizedAccessException: Access to the path '...' is denied.
Posted on 2011-02-11
This is a Web App in c# ASP.NET.
The user selects some criteria, then displays a Crystal Reports Report on the same page. I include the ability to export to .txt (tab or comma separated). But, when I try to save the file, I get the following error:
System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\Test\02112011.txt'.
(I'm sure you know but 'C:\Test\02112011.txt' is the file name on the local machine that I'm trying to save it to)
Someone had mentioned sending the file to the browser and making the user select a save location that way. How do I go about that? I want the page to still display the selection criteria and the Crystal Report.
Here's the code:
string ExportFile = "";
ExportFile = txtPath.Text.ToString();
float IsTxt = 0;
float Isfolder = 0;
IsTxt = ExportFile.IndexOf(".txt");
Isfolder = ExportFile.IndexOf(":\\");
if (IsTxt < 0)
ExportFile = ExportFile + ".txt";
if (Isfolder >= 0)
//go through with export
DiskFileDestinationOptions diskOpts = ExportOptions.CreateDiskFileDestinationOptions();
ExportOptions exportOpts = new ExportOptions();
exportOpts = report.ExportOptions;
if (rblExportType.SelectedValue.ToString() == "Tab")
exportOpts.ExportFormatType = ExportFormatType.TabSeperatedText;
exportOpts.ExportFormatType = ExportFormatType.CharacterSeparatedValues;
exportOpts.ExportDestinationType = ExportDestinationType.DiskFile;
diskOpts.DiskFileName = ExportFile;
exportOpts.ExportDestinationOptions = diskOpts;