繁体   English   中英

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

[英]How can I check the file I created already in the disk using savefileDialog in c# form?

是的,我知道这个问题已经存在,但他们似乎都没有回答我正在寻找的内容,大多数建议尝试 if (!Directory.Exists(path)),但此语句仅检查目录是否存在,并且不会如果它确实存在,则什么都没有。 但我的目标是在使用 savefileDialog 获取文件夹名称后,如果文件夹名称已存在,我想显示一个对话框以显示文件夹已存在,您想重写这样的文件吗?

private void saveToXML_Click(object sender, 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))
{
   MessageBox.Show("Directory already exists: '" + path + "'!");
}

这应该检查文件是否存在,而不是目录(确保包含扩展名):

if(System.IO.File.Exists(path))
{
   SomeFunction();
}

'SomeFunction' 应该表示一个文件存在,你想覆盖等。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM