.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

SaveFileDialog & Last Directory

18 REPLIES 18
SOLVED
Reply
Message 1 of 19
Ron_M
7192 Views, 18 Replies

SaveFileDialog & Last Directory

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.

18 REPLIES 18
Message 2 of 19
SENL1362
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;

Message 3 of 19
SENL1362
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
Message 4 of 19
khoa.ho
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.

 

 

Message 5 of 19
jeff
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
Message 6 of 19
SENL1362
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);
    }
}

 

Message 7 of 19
SENL1362
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();
}
}

 

Message 8 of 19
jeff
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
Message 9 of 19
SENL1362
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 🙂

 

 

Message 10 of 19
Ron_M
in reply to: SENL1362

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

Message 11 of 19
SENL1362
in reply to: Ron_M

AcadConfigTools are custom made specialised tools which will be useless to you. I wrote it in te previous mail to Jeff to show him an alternative to registry manipulations.

To get back to you're problem, what did you specify as the 4th argument in the initial post?

Message 12 of 19
Ron_M
in reply to: SENL1362

For the fourth aurgument I used "" which according to what I've been able to find is default and should return the last successful save path.

Message 13 of 19
SENL1362
in reply to: Ron_M

I doubt that. I think you have to specify a string constant, which will be the registry entry in which the settings of you're call will be saved.
The next time you call the dialog with the same string constant it will load and use the previous settings from that registry entry.

see "ExcelFileToLink" in this blog

http://through-the-interface.typepad.com/through_the_interface/2007/08/using-autocads-.html

Message 14 of 19
Ron_M
in reply to: SENL1362

Thank you for your assistance.  I've previously looked at Kean's blog at that very entry and others on this subject and have found it to be of no help.  I'll continue to research this topic.

Message 15 of 19
SENL1362
in reply to: Ron_M

do you have access to the registry?
if so have a look at the location mentioned by Jeff.
Message 16 of 19
Ron_M
in reply to: SENL1362

Resolved.  A lot easier than we thought.  Instead of using what Autodesk gives us I just used Net to create a registry key to store the last path to and then pull that in.  You'll notice that the last folder isn't the 4th argument, it's the second.

    Public Sub SaveDrawing()
        Dim LastSave As String = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\Autodesk\Autocad\R19.1\ACAD-D004:409\Forum-2014\", "LastSave", Nothing)
        Dim SFD As New Autodesk.AutoCAD.Windows.SaveFileDialog("", LastSave, "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 + "\"
            My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\Autodesk\Autocad\R19.1\ACAD-D004:409\Forum-2014\", "LastSave", DFP)
        Else : Crash()
        End If
    End Sub

 

Message 17 of 19
SENL1362
in reply to: Ron_M

Then credits to Jeff, it was his idea: Kudos++;
Thank you Ron for posting you're solution.
Message 18 of 19
Mendo123
in reply to: Ron_M

Hi, (newbie question)

 

Does anyone know how to filter revits ".rvt" files in the savefiledialog? everything I try throws an error.

I have no experience with the Filter property.

 

            string mydocumentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            string myDocumentSavedFilename = doc.Title + ".rvt";
            string myDocumentsFullPath = Path.Combine(mydocumentsPath, myDocumentSavedFilename);

            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.Title = "Save project";
            saveFileDialog1.Filter = ?????            
saveFileDialog1.InitialDirectory = myDocumentsFullPath; saveFileDialog1.DefaultExt = ".rvt"; saveFileDialog1.FileName = myDocumentsFullPath;

Thank you in advance

Message 19 of 19
SENL1362
in reply to: Mendo123

ofDlg.Filter = "Drawing (*.dwg)|*.dwg|DXF (*.dxf)|*.dxf|Drawing Template (*.dwt)|*.dwt|All Files (*.*)|*.*";

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost