AddExtension属性在C#2.0中如何工作? [英] How does the AddExtension property work in C# 2.0?

查看:95
本文介绍了AddExtension属性在C#2.0中如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打开一个保存文件对话框,让用户输入文件名,如果他们忘记了.csv扩展名,请附加它。

I want to open a save file dialog, have the user enter a filename, and if they forget the .csv extension, have it tacked on.

似乎SaveFileDialog的AddExtension属性可以工作,但不起作用。我什至将DefaultExt属性设置为.csv,仍然没有任何问题。我的文件保存得很好,但是没有扩展名,因此用户不能只双击该文件并在Excel中打开它。

It would seem that the SaveFileDialog AddExtension property would work, but its doesn't. I've even set the DefaultExt property to .csv, and still nothing gets tacked on. My file gets saved just fine, but sans extension, so the user can't just double click on the file and have it open in Excel.

我必须丢失明显的东西。这就是我所拥有的

I have to be missing something obvious. Here's what I've got

        SaveFileDialog sfd = new SaveFileDialog();
        sfd.DefaultExt = "*.csv";
        sfd.Filter = "Comma Separated(*.csv)|*.*";
        if (sfd.ShowDialog() == DialogResult.OK)
        {
            // Do my file saving
        }


推荐答案

尝试仅对<$ c使用 csv $ c> DefaultExt -同样,您应该使用 这个(它是 IDisposable ) :

Try just using "csv" for the DefaultExt - also, you should be using this (it is IDisposable):

        using (SaveFileDialog sfd = new SaveFileDialog())
        {
            sfd.AddExtension = true;
            sfd.DefaultExt = "csv";
            sfd.Filter = "Comma Separated(*.csv)|*.*";
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                // Do my file saving
            }
        }

这篇关于AddExtension属性在C#2.0中如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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