0

as you can see I am exporting crystal report to a PDF file but my problem is that the path of the crystal report RPT file and path of saving the oupt file (PDF File) since the path to folder at desktop is will be changed between PCs like

PC1 path code be

C:\Users\Xuser\Desktop

and on def rent pc is

C:\Users\XYPC\Desktop

so the path is not a static path .. anyway way out ?

Update:

now below code get the path to desktop but i am getting exception about access permissions which is access to path is denied

private void ExportToPDF()
    {

        ReportDocument cryrpt = new ReportDocument();

        try
        {
            cryrpt.Load("INVOICE_REP.rpt");

            ExportOptions CrExportOptions;
            DiskFileDestinationOptions CrDiskFileDestinationOptions = new DiskFileDestinationOptions();
            PdfRtfWordFormatOptions CrFormatTypeOptions = new PdfRtfWordFormatOptions();
            CrDiskFileDestinationOptions.DiskFileName = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
            MessageBox.Show(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory));
            CrExportOptions = cryrpt.ExportOptions;
            {
                CrExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
                CrExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;
                CrExportOptions.DestinationOptions = CrDiskFileDestinationOptions;
                CrExportOptions.FormatOptions = CrFormatTypeOptions;
            }
            cryrpt.Export();

            MessageBox.Show("Export Done");
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }
samer
  • 193
  • 5
  • 21

2 Answers2

1

You can use the Environment class.

Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
df_
  • 149
  • 4
  • 11
1

Use Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) to get the path to the desktop.

David Fulop suggested using Environment.GetFolderPath(Environment.SpecialFolder.Desktop). What's the difference between DesktopDirectory and Desktop?

According to MSDN:

  • Desktop is "the logical Desktop rather than the physical file system location."
  • DesktopDirectory is "the directory used to physically store file objects on the desktop."

Source for all of the above: https://msdn.microsoft.com/en-us/library/system.environment.specialfolder%28v=vs.110%29.aspx

Tanner Swett
  • 3,241
  • 1
  • 26
  • 32