C#资源文件管理

1.右键项目点属性;

2.点资源项,添加资源下拉框的添加现在文件,如下图:

3.直接上代码获取并复制到指定文件夹下:

private void button1_Click(object sender, EventArgs e)
{
    byte[] drawing1 = global::资源文件管理.Properties.Resources.Drawing1;
    byte[] gateway = global::资源文件管理.Properties.Resources.gateway;
    byte[] textgateway = global::资源文件管理.Properties.Resources.textgateway;//不需要后缀名
    //string smno = global::资源文件管理.Properties.Resources.smno;
    List<byte[]> list = new List<byte[]>();
    list.Add(drawing1);
    list.Add(gateway);
    list.Add(textgateway);
    DirectoryInfo dir = new DirectoryInfo(Application.StartupPath).Parent.Parent;
    string target = dir.FullName + "\Resources";//资源文件路径
    string[] files = Directory.GetFiles(target);//资源文件夹里的所有文件
    for (int i = 0; i < files.Length; i++)
    {
        string str = Path.GetFileName(files[i]);//textgateway.exe
        CopyFileTo(@"c:	ext", str, list[i]);
    }
    this.Close();
}

/// <summary>
/// 复制文件
/// </summary>
/// <param name="path"></param>
/// <param name="fileName"></param>
/// <param name="fileBuffer"></param>
private void CopyFileTo(string path, string fileName, byte[] fileBuffer)
{
    using (FileStream file = new FileStream(path + fileName, FileMode.Create))
    {
        file.Write(fileBuffer, 0, fileBuffer.Length);
    }
}

/// <summary>
/// 复制文件
/// </summary>
/// <param name="path"></param>
/// <param name="str"></param>
public static void CopyFileTo(string path, string str)
{
    using (StreamWriter sw = new StreamWriter(path))
    {
        sw.Write(str);
    }
}
原文地址:https://www.cnblogs.com/genesis/p/5145089.html