C#通过打开窗口打开文件,及新建文件的功能(另存为)

时间:2022-09-03 00:01:14

    打开shp格式的文件,把Filter改成你想打开的文件格式即可

/// <summary>

        /// 输入Shp文件(淹没区范围图)
        /// </summary>
        public string InputShp()
        {
            //  打开文件
            OpenFileDialog addShpFileDialog = new OpenFileDialog();
            addShpFileDialog.Filter = "Shp|*.shp";
            //判断打开文件窗口是否打开
            if (addShpFileDialog.ShowDialog() == DialogResult.OK)
            {
                if (addShpFileDialog.FileName != null)
                {
                    //得到Shp名称
                    inShpName = addShpFileDialog.FileName;
                }
                else
                { return null; }
            }
            else
            { return null; }
            return inShpName;
        }

      创建新文件,再次新建的为tif文件

        /// <summary>
        /// 输出Tif(水深图)
        /// </summary>
        public string OutputTif()
        {
            string outputTif = null;
            //打开保存文件对话框
            SaveFileDialog saveTifFileDialog = new SaveFileDialog();
            saveTifFileDialog.OverwritePrompt = true;//询问是否覆盖
            saveTifFileDialog.Filter = "*.tif|*.tif";
            saveTifFileDialog.DefaultExt = "tif";//缺省默认后缀名
            if (saveTifFileDialog.ShowDialog() == DialogResult.OK)
            {
                outputTif = saveTifFileDialog.FileName;
                //如果文件存在,删除重建
                if (File.Exists(outputTif))
                {
                    File.Delete(outputTif);
                }
                else { }
                System.IO.Stream fileStream = saveTifFileDialog.OpenFile();//保存
            }
            else
            { return null; }
            return outputTif;
        }