Set default report path with ReportSaveLoadPath

Stimulsoft Reports.NET discussion
Post Reply
patwolf
Posts: 90
Joined: Thu May 29, 2008 1:38 pm
Location: Los Angles

Set default report path with ReportSaveLoadPath

Post by patwolf »

Hi,

I'm creating a new report in code and would like when the user clicks the save button first that the default directory will be e.g.
string reportPath = @"C:\data\reports";
StiOptions.Preview.Window.ReportSaveLoadPath = reportPath;
StiOptions.Designer.ReportSaveLoadPath = reportPath;

I tried the above options but the directory always defaults to the last saved directory rather then @"C:\data\reports";

Even this doesn't work:
Environment.CurrentDirectory = reportPath;

What can I do please?

Thanks,
Patrick

PS: Using Version 2009.2.422.0
Edward
Posts: 2913
Joined: Fri Jun 09, 2006 4:02 am

Set default report path with ReportSaveLoadPath

Post by Edward »

Hi Patrick,

Yes, you are right. The properties:

StiOptions.Preview.Window.ReportSaveLoadPath = reportPath;
StiOptions.Designer.ReportSaveLoadPath = reportPath;

will work only for 'Save' buttons in the preview control and in the designer control respectively.

If you need control over 'Save as' button in the designer, then please reassign SaveAs event handler as follows:

Code: Select all

private void Form1_Load(object sender, EventArgs e)
{
    StiOptions.Viewer.Windows.ReportSaveLoadPath = "c:\\";
    StiOptions.Designer.ReportSaveLoadPath = "c:\\";
    StiReport report = new StiReport();
    report = new StiReport();

    // you can also hide Save As button
    //StiMainMenuService mainMenuService = StiMainMenuService.GetService();
    //mainMenuService.ShowFileReportSaveAs = false;

    StiDesigner.SavingReport += new StiSavingObjectEventHandler(On_SavingReport);
    StiDesigner.DontAskSaveReport = true;
    report.Design();

}


private void On_SavingReport(object sender, StiSavingObjectEventArgs e)
{
    StiReport report = (sender as StiDesignerControl).Report;
    
    // Checking if the filename to save is known or not
    // If you set static option StiDesigner.DontAskSaveReport = true, then e.SaveAs is true only when SaveAs button is pressed.
    if (e.SaveAs == true)
    {
        SaveFileDialog mySaveDialog = new SaveFileDialog();
        mySaveDialog.InitialDirectory = "D:\\";
        mySaveDialog.DefaultExt = ".mrt";
        mySaveDialog.FileName = Path.GetFileNameWithoutExtension((sender as StiDesignerControl).ReportFileName);
        do
        {
            if (mySaveDialog.ShowDialog() == DialogResult.OK)
            {
                if (Path.GetDirectoryName(mySaveDialog.FileName) == mySaveDialog.InitialDirectory)
                {
                    report.Save(mySaveDialog.FileName);
                    MessageBox.Show("Saving complete.");
                    (sender as StiDesignerControl).ReportFileName = mySaveDialog.FileName;
                    e.Processed = true;
                }
                else
                {
                    MessageBox.Show("You can save the report only in " + mySaveDialog.InitialDirectory.ToString() + " folder.");
                }
            }
            else
            {
                break;
            }
        }
        while (Path.GetDirectoryName(mySaveDialog.FileName) != mySaveDialog.InitialDirectory);
    }
    else
    {
        //Here is the code which fires after the filename had been set to the specified folder
        report.Save(report.ReportFile);
    }
}
Thank you.
patwolf
Posts: 90
Joined: Thu May 29, 2008 1:38 pm
Location: Los Angles

Set default report path with ReportSaveLoadPath

Post by patwolf »

Hi Edward,

thank you very much for your detailed answer... Here is what I ended up using in case it helps anybody else.

All the best,
Patrick

Code: Select all

StiDesigner.DontAskSaveReport = true;
StiDesigner.SavingReport += On_SavingReport;

private void On_SavingReport(object sender, StiSavingObjectEventArgs e)
{
    StiDesignerControl stiDesignerControl = sender as StiDesignerControl;
    if (stiDesignerControl == null)
        return;
    StiReport report = stiDesignerControl.Report;

    // Checking if the filename to save is known or not
    // If you set static option StiDesigner.DontAskSaveReport = true, then e.SaveAs is true only when SaveAs button is pressed.
    if (e.SaveAs || String.IsNullOrEmpty(report.ReportFile)) 
    {
        var saveDialog = new SaveFileDialog
            {
                CheckPathExists = true,
                OverwritePrompt = true,
                SupportMultiDottedExtensions = false,
                ValidateNames = true,
                Title = "Please choose a new report file",
                Filter = "Report files (*.mrt)|*.mrt",
                DefaultExt = ".mrt",
                AddExtension = true,
                AutoUpgradeEnabled = true,
                InitialDirectory = ReportFullPath,
                FileName = Path.GetFileNameWithoutExtension(stiDesignerControl.ReportFileName)
            };
        do
        {
            if (saveDialog.ShowDialog() == DialogResult.OK)
            {
                if (Path.GetDirectoryName(saveDialog.FileName) == saveDialog.InitialDirectory)
                {
                    report.Save(saveDialog.FileName);
                    stiDesignerControl.ReportFileName = saveDialog.FileName;
                    e.Processed = true;
                }
                else
                {
                    MessageBox.Show("You can save the report only in " + saveDialog.InitialDirectory.ToString() + " folder.");
                    saveDialog.FileName = Path.GetFileNameWithoutExtension(saveDialog.FileName);
                }
            }
            else
                break;
        }
        while (Path.GetDirectoryName(saveDialog.FileName) != saveDialog.InitialDirectory);
    }
    else
    {
        //Here is the code which fires after the filename had been set to the specified folder
        report.Save(report.ReportFile);
    }
}
meukoh
Posts: 6
Joined: Tue Jun 15, 2010 5:11 pm

Set default report path with ReportSaveLoadPath

Post by meukoh »

Thank you Patrick, very helpful code here.
Post Reply