Hello, I have the following function that opens the File "Save As" dialog box. It gets fed a drawing number and allows the users to browse to a folder where they want to save it. The problem is when a file already exists with the same name. A message box pops up letting the user know that the file already exists and do they want to overwrite it. The can pick yes to overwrite, but it doesn't really overwrite the old file, it seems like it opens it and then crashes when the rest of the program tries to run.
What I want to do is create a loop to determine if the file already exists. If it does not exist, then save the path and drawing name as usual.
If the drawing exists, I want to pop up a dialog giving two options; Overwrite, or Rename the current drawing. If they pick Overwrite, I can have the program delete the old existing drawing and then just save this one. If they pick Rename it will loop back to the "Save As" dialog giving the user the ability to change their drawing name (like add a "-1" to it.
Any help will be greatly appreciated.
Public Function GetFilePathAndName(ByVal dwgNumber) podDWG = ApplicationServices.Application.DocumentManager.Md
iActiveDocument podDB = podDWG.Database podEd = podDWG.Editor podPSO.InitialDirectory = "W:\Work_in_Process" podPSO.InitialFileName = dwgNumber podPSO.Filter = "AutoCAD DWG Files|*.dwg" podPFR = podEd.GetFileNameForSave(podPSO) Select Case podPFR.Status Case EditorInput.PromptStatus.OK SaveDwgName = podPFR.StringResult Case EditorInput.PromptStatus.Cancel MsgBox("User Cancelled.") End Select Return SaveDwgName End Function
I guess what I want to know is, is there a way to turn off the automatic message that pops up saying "The drawing already exists, do you want to overwrite?"
I want to supply my own message.
The dialog shouldn't actually do anything with the file. It simply returns a file name string. The dialog shows the message you mentioned if a file with the same name already exists. This should only effect the returned PromptFileNameResult.Status property. It's probably a good idea to check the PromptFileNameResult.ReadOnly property before you try to save the file.
If there is a crash it will have something to do with your code and how it accesses the existing file (i.e. a file sharing issue where the file is already open and cannot be accessed for save). It could be open by anyone if you are on a network, so you need to program defensively for this.
Do you know about the Autodesk.AutoCAD.Windows.SaveFileDialog class? The ObjectARX docs mention a SaveFileDialogFlags.DoNotWarnIfFileExists enum which you pass into the SaveFileDialog constructor.
If you haven't used the SaveFileDialog class before then you can check out the following posts. There are some slight differences between the two that are discussed in detail.
Using AutoCAD's file selection dialog from .NET:
Allowing a user to select from multiple file formats inside AutoCAD using .NET:
I'm sure this will do the trick for you. The only other alternative I can think of is to use a standard .NET SaveFileDialog class from either windows forms or wpf.
Thanks for the links. I believe what was happening was that if the file existed already, and the user pick "Yes" to overwrite, the file was being opened. Since the program creates blocks (and the blocks would already be created in the opened drawing) it would crash the program when AutoCad would ask if they wanted to re-define the block.
I've decided to go the simple route and just have the program delete the drawing name that the user enters before it saves the new drawing. If the drawing already exists, then it's deleted, if not, then it does nothing.