Delphi overwrite existing file on save dialog

本小妞迷上赌 提交于 2019-12-10 13:54:19

问题


I am using the TSaveDialog component to save a file from a button click. However, I am having trouble with saving on an existing file name. Generally, when you want to save over an existing file in Windows, a message box pops up asking you if you really want to overwrite the file. This is not the case with the TSaveDialog component and it will go ahead and write over the file without asking.

I was hoping there was a TSaveDialog function or event that I could use but I have not seen anything that looks like it handles this. So it could be that I simplely haven't found the correct method to use. If there is an event, I could use

if FileExists(saveDialog.FileName) then
  //and so forth

but the events TSaveDialog has are OnCanClose, OnClose, OnFolderChange, OnIncludeItem, OnSelectionChange, OnShow, OnTypeChange...

My question is, how do I pop up a message box to ask the user if they want to overwrite the existing file using the TSaveDialog component. Thanks.


回答1:


Use saveDialog.Options := saveDialog.Options + [ofOverwritePrompt] before you execute the dialog. Then it will ask if the user wants to overwrite the file or not.

But you do know that the TSaveDialog does not actually save the file, right? It just displays the standard Windows File Save dialog, and then returns the path the user chose. You have to save the file manually using this path, e.g. MyStringList.SaveToFile(saveDialog.FileName).



来源:https://stackoverflow.com/questions/2554046/delphi-overwrite-existing-file-on-save-dialog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!