配置文件读取路径,文件删除与复制功能

配置文件读取路径,文件删除与复制功能

欢迎交流和指导

你好! 这是你第一次使用 Markdown编辑器 所展示的欢迎页。如果你想学习如何使用Markdown编辑器, 可以仔细阅读这篇文章,了解一下Markdown的基本语法知识。

form1和form2图片

居中的图片:

在这里插入图片描述在这里插入图片描述

主页面FORM1代码块

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 烧录分发程序
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

    private void button1_Click(object sender, EventArgs e)
    {
        Form2 f2 = new Form2();
        //1.从设置中获取路径地址,方法在IniFiles 
        string sourceFilePath = f2.pathret1();
        string destinationFilePath = f2.pathret2();

        //2.清空文件夹
        fileDelete(sourceFilePath);

        //3.复制文件到指定目录下
        fileCopy(destinationFilePath + textBox1.Text,sourceFilePath + textBox1.Text);

        

    }
    /// <summary>
    ///   清空文件夹的方法
    /// </summary>
    /// <param name="pathDelete"></param>

    private void  fileDelete(string pathDelete) 
    {
        try
        {

            DirectoryInfo dir = new DirectoryInfo(pathDelete);
            FileSystemInfo[] fileinfo = dir.GetFileSystemInfos();  //返回目录中所有文件和子目录
            foreach (FileSystemInfo i in fileinfo)
            {
                if (i is DirectoryInfo)            //判断路径是否有文件夹
                {
                    DirectoryInfo subdir = new DirectoryInfo(i.FullName);
                    subdir.Delete(true);          //删除子目录和文件
                    textBox2.Text = ("文件夹下的内容删除成功!");
                }
                else
                {
                    File.Delete(i.FullName);      //删除指定文件
                    
                    textBox2.Text = "更换烧录文件成功!";
                }

            }
        }
        catch (Exception i)
        {
            textBox2.Text = "清空的文件夹路径不对!";
            MessageBox.Show("清空的文件夹路径不对!","提示");
        }
        


    }


    /// <summary>
    ///程序存放路径  复制到  烧录程序路径(源路径)
    /// </summary>
    /// <param name="sourceFilePath1"></param>
    /// <param name="destinationFilePath2"></param>
    private void fileCopy(string sourceFilePath1, string destinationFilePath2) 
    {
        try
        {
            File.Copy(sourceFilePath1, destinationFilePath2);
            textBox2.Text = "烧录文件:  "+textBox1.Text+"  \t\n  ===>>更换成功!";
        }
        catch (Exception i)
        {
            MessageBox.Show("未找到此文件 \t\n 输入的文件名不合法或者不对!","提示");
            textBox2.Text = "未找到此文件 ,输入的文件名不合法或者不对!";
        }
        

    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        

    }

    /// <summary>
    /// 设置 按钮
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void button2_Click(object sender, EventArgs e)
    {
        Form2 f2 = new Form2();
        f2.Show();
        
        

    }

    private void button3_Click(object sender, EventArgs e)
    {

    }

    private void textBox2_TextChanged(object sender, EventArgs e)
    {

    }

    private void Form1_Load(object sender, EventArgs e)
    {
        textBox1.Focus();
        textBox2.Enabled = false;
    }

}

}

form2 代码块 调用 配置文件获取路径------------------------------------------------------

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 烧录分发程序
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}

    private void button2_Click(object sender, EventArgs e)
    {
 
        this.Close();

    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {

    }
    //在当前程序路径创建
    private string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config.ini");
    private void textBox2_TextChanged(object sender, EventArgs e)
    {

    }
    
    private void button1_Click(object sender, EventArgs e)
    {
        #region 创建INI文件
        FileStream INIFile = File.Create(filePath);
        INIFile.Close();
        #endregion



        //写入节点1
        IniFiles.Write("烧录程序使用路径", "地址1", textBox1.Text, filePath);
        IniFiles.Write("烧录程序获取路径", "地址2", textBox2.Text, filePath);
        
        textBox1.Enabled = false;
        textBox2.Enabled = false;
      
    }

    public string pathret1()
    {

        return IniFiles.Read("烧录程序使用路径", "地址1", textBox1.Text, filePath); ;

    }

    public string pathret2()
    {

        return IniFiles.Read("烧录程序获取路径", "地址2", textBox2.Text, filePath); ;

    }

    private void Form2_Load(object sender, EventArgs e)
    {
        textBox1.Text = IniFiles.Read("烧录程序使用路径", "地址1", textBox1.Text, filePath);
        textBox2.Text = IniFiles.Read("烧录程序获取路径", "地址2", textBox2.Text, filePath);
    }
}

}

配置文件的代码块-----------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace 烧录分发程序
{
///
/// INI文件操作类
///
public class IniFiles
{

        /// <summary>
        /// 为INI文件中指定的节点取得字符串
        /// </summary>
        /// <param name="lpAppName">欲在其中查找关键字的节点名称</param>
        /// <param name="lpKeyName">欲获取的项名</param>
        /// <param name="lpDefault">指定的项没有找到时返回的默认值</param>
        /// <param name="lpReturnedString">指定一个字串缓冲区,长度至少为nSize</param>
        /// <param name="nSize">指定装载到lpReturnedString缓冲区的最大字符数量</param>
        /// <param name="lpFileName">INI文件完整路径</param>
        /// <returns>复制到lpReturnedString缓冲区的字节数量,其中不包括那些NULL中止字符</returns>
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, int nSize, string lpFileName);

        /// <summary>
        /// 修改INI文件中内容
        /// </summary>
        /// <param name="lpApplicationName">欲在其中写入的节点名称</param>
        /// <param name="lpKeyName">欲设置的项名</param>
        /// <param name="lpString">要写入的新字符串</param>
        /// <param name="lpFileName">INI文件完整路径</param>
        /// <returns>非零表示成功,零表示失败</returns>
        [DllImport("kernel32")]
        private static extern int WritePrivateProfileString(string lpApplicationName, string lpKeyName, string lpString, string lpFileName);

        /// <summary>
        /// 读取INI文件值
        /// </summary>
        /// <param name="section">节点名</param>
        /// <param name="key">键</param>
        /// <param name="def">未取到值时返回的默认值</param>
        /// <param name="filePath">INI文件完整路径</param>
        /// <returns>读取的值</returns>
        public static string Read(string section, string key, string def, string filePath)
        {
            StringBuilder sb = new StringBuilder(1024);
            GetPrivateProfileString(section, key, def, sb, 1024, filePath);
            return sb.ToString();
        }

        /// <summary>
        /// 写INI文件值
        /// </summary>
        /// <param name="section">欲在其中写入的节点名称</param>
        /// <param name="key">欲设置的项名</param>
        /// <param name="value">要写入的新字符串</param>
        /// <param name="filePath">INI文件完整路径</param>
        /// <returns>非零表示成功,零表示失败</returns>
        public static int Write(string section, string key, string value, string filePath)
        {
            CheckPath(filePath);
            return WritePrivateProfileString(section, key, value, filePath);
        }

        /// <summary>
        /// 删除节
        /// </summary>
        /// <param name="section">节点名</param>
        /// <param name="filePath">INI文件完整路径</param>
        /// <returns>非零表示成功,零表示失败</returns>
        public static int DeleteSection(string section, string filePath)
        {
            return Write(section, null, null, filePath);
        }

        /// <summary>
        /// 删除键的值
        /// </summary>
        /// <param name="section">节点名</param>
        /// <param name="key">键名</param>
        /// <param name="filePath">INI文件完整路径</param>
        /// <returns>非零表示成功,零表示失败</returns>
        public static int DeleteKey(string section, string key, string filePath)
        {
            return Write(section, key, null, filePath);
        }

        /// <summary>
        /// 检查文件是否存在
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        private static void CheckPath(string filePath)
        {
            if (string.IsNullOrWhiteSpace(filePath) || !File.Exists(filePath))
            {
                throw new ArgumentNullException("filePath");
            }
        }
    
}

}

导入

如果你想加载一篇你写过的.md文件,在上方工具栏可以选择导入功能进行对应扩展名的文件导入,
继续你的创作。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值