.NET

Reply
Valued Contributor
Ron_M
Posts: 75
Registered: ‎12-09-2013
Message 1 of 17 (244 Views)
Accepted Solution

SaveFileDialog & Last Directory

244 Views, 16 Replies
07-30-2014 04:01 AM

I'm having an issue with the SaveFileDialog always returning to the .dll working path instead of the last path saved to.  I know that the 4th argument sets this but after trying for several days to come up with the correct setting and failing to succeed I come to the Forum for assistance.  My code below:

 

    Public Sub SaveDrawing()
        Dim acDocMgr As DocumentCollection = Application.DocumentManager
        Dim SFD As New SaveFileDialog("", "", "dwg", "", Autodesk.AutoCAD.Windows.SaveFileDialog.SaveFileDialogFlags.AllowAnyExtension)
        If SFD.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
            strName = Path.GetFileNameWithoutExtension(SFD.Filename)
            strPath = Path.GetDirectoryName(SFD.Filename)
            DFN = strPath + "\" + strName + ".dwg"
            DFP = strPath + "\"
        Else : Crash()
        End If
    End Sub

It would seem that I need somethig along the lines of System.IO.Path but I can't seem to get that to work.  I had thought the default was to the last saved path but that isn't how this is working.

I believe all that info is in th registry

 

I think you can use UserConfigurationManager.OpenDialogSection method to get the IConfigurationSection that reads and writes information in the dialog section of the registry.

 

And I think for example in 2013 it is in registry at

 

HKEY_CURRENT_USER\Software\Autodesk\AutoCAD\R19.0\ACAD-B001:409\Profiles\[CurrentProfileName\Dialogs\Save Drawing As

 

 

I would open regedit and save a drawing in a folder with a unique or uncommon name then the right click on upper level folder in regedit and export it to file.

 

I would open with a text editor then do a search for folder name and drawing and see where it all shows up  then  save drawing in different locations and confirm value updates in registry with each save.

 

 

 

 

 

 

 

Mentor
SENL1362
Posts: 232
Registered: ‎07-20-2011
Message 2 of 17 (222 Views)

Re: SaveFileDialog & Last Directory

07-30-2014 12:17 PM in reply to: Ron_M

I don't have a working .net sample with me but from my tips and trick file this c# sample may help:

 

C#/Microsoft/FileOpen/Desktop/OpenFileDialog/Common/SaveFile/FileSave
using Microsoft.Win32;
...
//Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
OpenFileDialog ofDlg = new OpenFileDialog();
ofDlg.InitialDirectory=Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
ofDlg.FileName = "";
ofDlg.DefaultExt = ".dwg";
ofDlg.Filter = "Drawing (*.dwg)|*.dwg|DXF (*.dxf)|*.dxf|Drawing Template (*.dwt)|*.dwt|All Files (*.*)|*.*";
Nullable<bool> ofDlgRes = ofDlg.ShowDialog();
if (ofDlgRes != true)
return;
string dwgPathname = ofDlg.FileName;

Mentor
SENL1362
Posts: 232
Registered: ‎07-20-2011
Message 3 of 17 (221 Views)

Re: SaveFileDialog & Last Directory

07-30-2014 12:31 PM in reply to: Ron_M
May be the RestoreDirectory may have something to do with the return to that folder location


Dim saveFileDialog1 As New SaveFileDialog
saveFileDialog1.InitialDirectory = "C:\"
saveFileDialog1.Title = "Save text Files"
saveFileDialog1.CheckFileExists = True
saveFileDialog1.CheckPathExists = True
saveFileDialog1.DefaultExt = "txt"
saveFileDialog1.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"
saveFileDialog1.FilterIndex = 2
saveFileDialog1.RestoreDirectory = True

If (saveFileDialog1.ShowDialog() = DialogResult.OK) Then
TextBox1.Text = saveFileDialog1.FileName
End If
Mentor
khoa.ho
Posts: 224
Registered: ‎09-15-2011
Message 4 of 17 (212 Views)

Re: SaveFileDialog & Last Directory

07-30-2014 01:22 PM in reply to: SENL1362

RestoreDirectory is the property of System.Windows.Forms.SaveFileDialog. The AutoCAD class Autodesk.AutoCAD.Windows.SaveFileDialog does not have it. They are two different dialogs. AutoCAD Save Dialog has more items on the left to connect to FTP and Buzzaw while the normal Windows Save Dialog does not have.

 

In the following code snippet:

 

using SaveFileDialog = Autodesk.AutoCAD.Windows.SaveFileDialog;
...
SaveFileDialog SFD = new SaveFileDialog("", "", "dwg", "",
	SaveFileDialog.SaveFileDialogFlags.AllowAnyExtension |
	SaveFileDialog.SaveFileDialogFlags.ForceDefaultFolder);

 

Whether I include SaveFileDialog.SaveFileDialogFlags.ForceDefaultFolder or not include it, AutoCAD still opens the AutoCAD working path instead of the last saved path. I also don't know how to set the last saved location using .NET API. But AutoCAD can do it with its native code.

 

 

Valued Mentor
jeff
Posts: 338
Registered: ‎05-12-2009
Message 5 of 17 (203 Views)

Re: SaveFileDialog & Last Directory

07-30-2014 05:09 PM in reply to: khoa.ho

I believe all that info is in th registry

 

I think you can use UserConfigurationManager.OpenDialogSection method to get the IConfigurationSection that reads and writes information in the dialog section of the registry.

 

And I think for example in 2013 it is in registry at

 

HKEY_CURRENT_USER\Software\Autodesk\AutoCAD\R19.0\ACAD-B001:409\Profiles\[CurrentProfileName\Dialogs\Save Drawing As

 

 

I would open regedit and save a drawing in a folder with a unique or uncommon name then the right click on upper level folder in regedit and export it to file.

 

I would open with a text editor then do a search for folder name and drawing and see where it all shows up  then  save drawing in different locations and confirm value updates in registry with each save.

 

 

 

 

 

 

 

You can also find your answers @ TheSwamp
Mentor
SENL1362
Posts: 232
Registered: ‎07-20-2011
Message 6 of 17 (193 Views)

Re: SaveFileDialog & Last Directory

07-30-2014 10:41 PM in reply to: jeff

This works for me, that is i can define the default foldername without hacking the registry

Results:

Command: TESTSAVEAS

--> FileDialog opens in c:\temp

--> Browse to d:\Apldata\AutoCAD

Selected dxf Pathname==D:\AplData\AutoCAD\test.dxf

 

Command: TESTSAVEAS

--> FileDialog opens in c:\temp

Selected dxf Pathname==C:\temp\test.dxf

 

 

[CommandMethod("TestSaveAs")]
public static void TestSaveAs()
{
    Document doc = Application.DocumentManager.MdiActiveDocument;
    Editor ed = doc.Editor;

    try
    {
        //using AcWin = Autodesk.AutoCAD.Windows;
        AcWin.SaveFileDialog.SaveFileDialogFlags saveFlags = AcWin.SaveFileDialog.SaveFileDialogFlags.DoNotTransferRemoteFiles;
        saveFlags |= AcWin.SaveFileDialog.SaveFileDialogFlags.ForceDefaultFolder;
        string defaultDxfPathname = @"c:\temp\test.dxf";
        //or assign only the ParentFoldername as the default Folder to store the file in, e.g.
        // string defaultDxfPathname = @"c:\temp\";

        AcWin.SaveFileDialog selDxf = new AcWin.SaveFileDialog("Select DXF file", defaultDxfPathname, "dxf", "TestSaveAs", saveFlags);
        System.Windows.Forms.DialogResult dlgRes = selDxf.ShowDialog();
        if (dlgRes != System.Windows.Forms.DialogResult.OK)
            return;
        string dxfPathname = selDxf.Filename;
        ed.WriteMessage("\n Selected dxf Pathname=={0}", dxfPathname);
    }
    catch (System.Exception ex)
    {
        ed.WriteMessage("\n {0}", ex.Message);
    }
}

 

Mentor
SENL1362
Posts: 232
Registered: ‎07-20-2011
Message 7 of 17 (189 Views)

Re: SaveFileDialog & Last Directory

07-30-2014 10:56 PM in reply to: SENL1362

This is another sample found and used a few years ago.

File Select Dialog opend from within a Form and using ED to prevent flashing the screen

 

private void SelectFile_Click(object sender, EventArgs e)
{
Document doc = Acad.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
using (EditorUserInteraction preventFlikkering = ed.StartUserInteraction(this))
{
PromptOpenFileOptions selGenDxf = new PromptOpenFileOptions("Select DXF");
selGenDxf.DialogCaption = "Select GEN dxf";
selGenDxf.InitialDirectory = @"C:\Apldata\AutoCAD\Smallworld_LS";
selGenDxf.Filter = "DXF File|*.dxf|AutoCAD File|*.dwg";
PromptFileNameResult selRes = ed.GetFileNameForOpen(selGenDxf);
if (selRes.Status != PromptStatus.OK) return;
this.tbFilename.Text = selRes.StringResult;
ed.WriteMessage("\nUser Selected: {0}", selRes.StringResult);
ed.Regen();
preventFlikkering.End();
}
}

 

Valued Mentor
jeff
Posts: 338
Registered: ‎05-12-2009
Message 8 of 17 (185 Views)

Re: SaveFileDialog & Last Directory

07-31-2014 12:30 AM in reply to: SENL1362

SENL1362,

 

The OP is wanting the dialog to open to the location of previous succuessful save. The registry is where AutoCAD saves that information and can be acessed between sessions.

 

Without "hacking the registry" can your make your example produce the following results

 

Command: TESTSAVEAS

--> FileDialog opens in c:\temp

--> Browse to d:\Apldata\AutoCAD

Selected dxf Pathname==D:\AplData\AutoCAD\test.dxf

 

CLOSE AUTOCAD

RESTART AUTOCAD

 

Command: TESTSAVEAS

--> FileDialog opens in D:\AplData\AutoCAD

--> Browse to C:\temp\New Folder

Selected dxf Pathname==C:\temp\New Folder\test.dxf

 

CLOSE AUTOCAD

RESTART AUTOCAD

 

Command: TESTSAVEAS

--> FileDialog opens in C:\temp\New Folder

 

 

 

You can also find your answers @ TheSwamp
Mentor
SENL1362
Posts: 232
Registered: ‎07-20-2011
Message 9 of 17 (178 Views)

Re: SaveFileDialog & Last Directory

07-31-2014 01:18 AM in reply to: jeff

Sure:
string lastOpenPathname = AcadConfigTools.Settings.Section["General"]["LastOpenLocation"];
...
lastOpenPathname=Path.GetDirectoryName(selectedLocation);
AcadConfigTools.Settings.Section["General"]["LastOpenLocation"]=lastOpenPathname ;

 

 


The AcadConfigTools.Settings is the integration of this INI file handling:
http://www.codeproject.com/Articles/318783/Simplified-INI-Handling

 

For read only ini file handling this is a really nice tool:
http://michael.chanceyjr.com/useful-code/ini-file-processing/


For the usage of the AutoCAD Dialogs,have a look at this article as well
http://through-the-interface.typepad.com/through_the_interface/2007/08/using-autocads-.html
Look for the comments on the fourth argument, it is a kind of automated hacking of the registry :smileyhappy:

 

 

Valued Contributor
Ron_M
Posts: 75
Registered: ‎12-09-2013
Message 10 of 17 (168 Views)

Re: SaveFileDialog & Last Directory

07-31-2014 04:57 AM in reply to: SENL1362

What would be the Import on AcadConfigTools?  Or if possible help me integrate into my posted code?

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.