C# WinForm中如何将文件保存在文件夹中或如何新建文件夹来保存

上一章节,我们进行了文件的保存到默认地方,这一章节我们将实现如何将文件保存自主新建的文件夹中。

 //文件IO流
            DialogResult dr = MessageBox.Show("是否选择默认文件夹保存?", "信息提示", MessageBoxButtons.YesNo);
            if (dr == System.Windows.Forms.DialogResult.Yes)  //默认的文件夹
            {
                FileStream fs = new FileStream(@"E:\" + tabName + "Dal.cs", FileMode.Create);
                StreamWriter sw = new StreamWriter(fs, Encoding.Default);
                sw.Write(str); //str为文本内同
                sw.Close();
                fs.Close();
                MessageBox.Show("操作成功," + tabName + "DAL.cs文件已默认保存", "信息提示");
            }
            else
            {
                FolderBrowserDialog folder = new FolderBrowserDialog();  //查找文件夹中
                if (folder.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    string aa = folder.SelectedPath; //文件夹路径
                    char asicc1 = (char)92; 
                    FileStream fs = new FileStream(@"" + aa + "" + asicc1 + "" + tabName + "Dal.cs", FileMode.Create);
                    StreamWriter sw = new StreamWriter(fs, Encoding.Default);
                    sw.Write(str);
                    sw.Close();
                    fs.Close();
                    MessageBox.Show("操作成功," + tabName + "DAL.cs文件已保存至" + aa + "文件夹下", "信息提示");
                }
            }

********************************************************************************

【方法二】可以不通过一个【浏览...】那样,你可以直接在点击【确定】按钮的同时,弹出一个路径选择框。具体您在后台的代码中,也就是在【确定】按钮的Click事件中输入类似以下的一段代码即可。简单方便。

>>>拖入控件【FolderBrowserDialog】,命名为fdb

 DialogResult result = fbd.ShowDialog();
if (result == DialogResult.OK)
{
     path = fbd.SelectedPath;  //获取文件路径
     //.......
}

//

方法1: 

C#代码  
if (Directory.Exists("D:\\我的文档\\桌面"))   
{   
    MessageBox.Show("该文件夹存在");   
}   
else  
{   
    MessageBox.Show("该文件夹不存在");   
}     
方法2: 

C#代码  
DirectoryInfo TheFolder = new DirectoryInfo("d:\\pic");   
if (TheFolder.Exists)   
{   
    MessageBox.Show("进来了");   
}   
else  
{   
    MessageBox.Show("没进来");   
}  
"d:\\pic"可以这样变成路径 
@"d:\pic" 
也就是说转义符可以用@来代替 

新建文件夹: 

C#代码  
if (!Directory.Exists(@txtFileSaveDir.Text))  //若文件夹不存在则新建文件夹   
{   
    Directory.CreateDirectory(@txtFileSaveDir.Text); //新建文件夹   
}

****【实例研究】**-----------+++++++++++++++++++

#region 文件流IO PK 选择文件路径
                    //文件IO流
                    DialogResult dr = MessageBox.Show("是否选择默认文件夹保存?", "小德温馨提示您", MessageBoxButtons.YesNo);
                    if (dr == System.Windows.Forms.DialogResult.Yes)
                    {
                        string ProjectPath = @"D:\我的文档\桌面\新建文件夹 (3)\" + cmbDataBaseName.Text;
                        if (!Directory.Exists(ProjectPath))  //若文件夹不存在则新建文件夹   
                        {
                            DialogResult DrMoren = MessageBox.Show("该文件夹不存在\n是否重新创建默认文件夹?", "小德温馨提示您", MessageBoxButtons.YesNo);
                            if (DrMoren==System.Windows.Forms.DialogResult.Yes)
                            {
                                Directory.CreateDirectory(ProjectPath); //新建文件夹   
                                FileStream fs = new FileStream(ProjectPath + "\\" + txtTB_Name + ".cs", FileMode.Create);
                                StreamWriter sw = new StreamWriter(fs, Encoding.Default);
                                sw.Write(txtModelCoder);
                                sw.Close();
                                fs.Close();
                                success.ShowMessage("操作成功," + txtTB_Name + ".cs文件已保存至" + cmbDataBaseName.Text + "文件夹");
                            } 
                            else
                            {
                                return;
                            }
                        }
                    }
                    else
                    {
                        FolderBrowserDialog folder = new FolderBrowserDialog();
                        if (folder.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                        {
                            string aa = folder.SelectedPath;
                            char asicc1 = (char)92; //(char)92值的是 \ 
                            FileStream fs = new FileStream(@"" + aa + "" + asicc1 + "" + txtTB_Name + "Model.cs", FileMode.Create);
                            StreamWriter sw = new StreamWriter(fs, Encoding.Default);
                            sw.Write(txtModelCoder);
                            sw.Close();
                            fs.Close();
                            success.ShowMessage("操作成功," + txtTB_Name + "Model.cs文件已保存至" + aa + "文件夹下");
                        }
                    }
                    #endregion


  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值