我找到了解决问题的方法,但我不知道正确的路径。
select random file from directory
public string getrandomfile2(string path)
{
string file = null;
if (!string.IsNullOrEmpty(path))
{
var extensions = new string[] { ".png", ".jpg", ".gif" };
try
{
var di = new DirectoryInfo(path);
var rgFiles = di.GetFiles("*.*").Where( f => extensions.Contains( f.Extension.ToLower()));
Random R = new Random();
file = rgFiles.ElementAt(R.Next(0,rgFiles.Count())).FullName;
}
// probably should only catch specific exceptions
// throwable by the above methods.
catch {}
}
return file;
}
我使用这些路径,但没有一个可行:
"/Images/defaultImages"
"~/Images/defaultImages"
"Images/defaultImages"
正确的道路是什么?
答案 0 :(得分:0)
尝试
Server.MapPath("~/Images/defaultImages")
还要确保您的defaultImages
文件夹包含图像文件