如何使用c#格式的savefileDialog检查已在磁盘中创建的文件?

问题描述 投票:-1回答:1

是,我知道这个问题已经存在,但是它们似乎都没有回答我真正要寻找的内容,大多数建议尝试尝试(!Directory.Exists(path)),但是此语句仅检查目录是否存在。如果存在,则不执行任何操作。 但是我的目标是在使用savefileDialog获得文件夹的名称之后,如果该文件夹名称已经存在,我想显示一个对话框以显示该文件夹已经存在,您想重写这样的文件内容吗?>

private void saveToXML_Click(对象发送者,EventArgs e){

        try
        {

            //Here we create a directory and including all the xml files in that directory 
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                //Combining my path so its pointing at the correct directory
                string path = System.IO.Path.Combine(Environment.CurrentDirectory,  saveFileDialog1.FileName);



                if (!Directory.Exists(path))
                {
                    //Initialzing where we want to create directory
                    Directory.CreateDirectory(path);

                    //Now we are seriazling all the List instances to an XML file and storing all the XML file in the user created directory 
                    using (Stream stream = new FileStream(path + "\\TARA_Assumptions.XML", FileMode.Create, FileAccess.Write, FileShare.None))
                    {
                        XmlSerializer serializer = new XmlSerializer(typeof(List<Introduction>));
                        serializer.Serialize(stream, AssumptionInstances);
                        stream.Close();
                    }

                    MessageBox.Show("Saved Assumptions OBJ to XML");

是,我知道这个问题已经存在,但是他们似乎都没有回答我真正要寻找的东西,大多数建议尝试使用if(!Directory.Exists(path)),但是此语句仅检查...”。 >

c# visual-studio windows-forms-designer
1个回答
0
投票

类似以下内容:

if (Directory.Exists(path))
{
   MessageBox.Show("Directory already exists: '" + path + "'!");
}
© www.soinside.com 2019 - 2024. All rights reserved.