更改文件的名称而不是SaveFileDialog.OverwritePrompt [英] change the name of file instead of SaveFileDialog.OverwritePrompt

查看:139
本文介绍了更改文件的名称而不是SaveFileDialog.OverwritePrompt的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好;

我的Windows窗体应用用户可以使用saveFileDialog将图像添加到硬盘中的文件夹中。

如何通过在图像名称中添加变量整数来更改图像名称而不是OverwritePrompt警告?

我的意思是如何自动更改名称(通过添加数字而不是用户试图覆盖现有文件?

Hi All;
in my windows form app user can add image in a folder in hard drive with saveFileDialog.
How can I change the name of image instead of OverwritePrompt alert with adding a variable integer to the name of image?
I mean how can I automatically change the name (by adding a number) instead of the user tries to overwrite an existing file?

推荐答案

你好阿里



请首先从保存对话框中获取带有 FileName 属性的文件名和路径,并在检查文件后文件是否存在,您应该更改文件名并再次保存;)



Hi Ali

Please first get the file name and path with FileName property from Save dialog and after check the file if the file is exist you should change the file name and save again ;)

private void Save_Click(object sender, EventArgs e)
   {
       Random rnd = new Random(0);
       string filename = saveFileDialog1.FileName;
       if (System.IO.File.Exists(filename))
           filename += rnd.Next(1, 1000000);
   }





最好的问候。



Best Regards.


设置 SaveFileDialog.OverwritePrompt 属性为false,它不会向用户显示任何进一步的信息。

对话框本身根本不影响文件:即使文件存在它不会受到对话框的任何影响,只能通过你的代码。



所以在ShowDialog调用之后,检查文件是否存在:

Set the SaveFileDialog.OverwritePrompt property to false, and it will not display any further information to the user.
The dialog itself does not affect files at all: even if the file exists it will not be affected in any way by the dialog, only by your code.

So after the ShowDialog call, check if the file exists:
SaveFileDialog sfd = new SaveFileDialog();
sfd.OverwritePrompt = false;
if (sfd.ShowDialog() == DialogResult.OK)
    {
    if (File.Exists(sfd.FileName))
        {
        // Change the name
        ...
        }
    }



您可以自己添加号码根据需要。

(我可能会将现有文件重命名为existingFileName.00001.originalextension,而不是在新保存的文件中添加一个数字,但那只是我)


You can then add the number yourself as needed.
(I would probably rename the existing file to "existingFileName.00001.originalextension" rather than adding a number to the newly saved file, but that''s just me)


这篇关于更改文件的名称而不是SaveFileDialog.OverwritePrompt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆