使用 ConfigurationManager.AppSettings可以映射到工程中web.config对应的值,
用于保存文件到指定的路径下,或者在指定的路径下删除文件
web.config中的一段代码:
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
<add key="DataPath" value="D:/UIH/UIH_uPR/data" />
<add key="HisClassName" value="PETCT" />
</appSettings>
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);
}