Mik*_*ynn 7 c# regex asp.net file-io
我可以使用正则表达式读取磁盘而不是这样做三次吗?
var path = HttpContext.Current.Server.MapPath(string.Format("~/Assets/Images/{0}.png", id));
if (!File.Exists(path))
{
path = HttpContext.Current.Server.MapPath(string.Format("~/Assets/Images/{0}.jpg", id));
if (!File.Exists(path))
{
path = HttpContext.Current.Server.MapPath(string.Format("~/Assets/Images/{0}.gif", id));
Run Code Online (Sandbox Code Playgroud)
您可以获取目录位置中所有文件的列表,并使用 LINQ 查找路径,方法是Directory.EnumerateFiles:
var files = Directory.EnumerateFiles(Server.MapPath("~/Assets/Images/"));
if(files.Contains(string.Format("{0}.png", id))
{
}
Run Code Online (Sandbox Code Playgroud)
根据文件的数量,它可能会产生比您的解决方案更好的结果。
| 归档时间: |
|
| 查看次数: |
590 次 |
| 最近记录: |