【问题标题】:how to Save a file at folder at the desktop no mater what the path is? [duplicate]无论路径是什么,如何将文件保存在桌面文件夹中? [重复]
【发布时间】:2016-08-20 23:33:36
【问题描述】:

如您所见,我正在将水晶报表导出为 PDF 文件 但我的问题是水晶报告 RPT 文件的路径和保存输出文件(PDF 文件)的路径,因为桌面文件夹的路径将在 PC 之间更改,如

PC1路径代码为

C:\Users\Xuser\Desktop

并且默认租用电脑是

C:\Users\XYPC\Desktop

所以路径不是静态路径..无论如何出路?

更新:

现在下面的代码获取到桌面的路径,但我得到关于访问权限的异常,即访问路径被拒绝

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());
        }
    }

【问题讨论】:

  • System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop)

标签: c# path crystal-reports


【解决方案1】:

您可以使用 Environment 类。

Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

【讨论】:

    【解决方案2】:

    使用Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) 获取桌面的路径。

    David Fulop 建议使用Environment.GetFolderPath(Environment.SpecialFolder.Desktop)DesktopDirectoryDesktop 有什么区别?

    根据 MSDN:

    • Desktop 是“逻辑桌面而不是物理文件系统位置”。
    • DesktopDirectory 是“用于在桌面上物理存储文件对象的目录。”

    以上所有内容的来源:https://msdn.microsoft.com/en-us/library/system.environment.specialfolder%28v=vs.110%29.aspx

    【讨论】:

    • 很好,但现在我收到一条关于访问权限的错误消息,即访问路径被拒绝
    • 您能否编辑您的问题以包含产生该错误的代码?
    • 我已经更新了我的代码
    • 我厌倦了以管理员身份运行 exe 文件,但仍然显示相同的错误
    猜你喜欢
    • 2016-03-13
    • 2015-11-12
    • 1970-01-01
    • 2019-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-24
    相关资源
    最近更新 更多