ConfigurationManager.AppSettings

使用 ConfigurationManager.AppSettings可以映射到工程中web.config对应的值,
用于保存文件到指定的路径下,或者在指定的路径下删除文件
web.config中的一段代码:







public void SavePicture(byte[] data)
{
if (null == data)
{
return;
}

    //将文件保存到计算机硬盘中
    //["DataPath"],DataPath对应webconfig中Key值为DataPath
     var path = ConfigurationManager.AppSettings["DataPath"] +"/Picture/" + PreRegistrationIdFk + "/"+ PictureUid + ".jpg";
        var dir = Path.GetDirectoryName(path);
        if (null != dir && !Directory.Exists(dir))
        {
            Directory.CreateDirectory(dir);
        }
        using (var fs = new FileStream(path, FileMode.CreateNew))
        {
            fs.Write(data, 0, data.Length);
        }
        PicturePath = path;
    }

    //删除硬盘中的资源文件
    public void DeletePicture()
    {
    var path = ConfigurationManager.AppSettings["DataPath"] + "/Picture/" + PreRegistrationIdFk + "/" + PictureUid + ".jpg";
     File.Delete(path);
    }

你可能感兴趣的:(ConfigurationManager.AppSettings)