Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# asp从具有服务器路径的目录中选择随机文件_C#_Asp.net - Fatal编程技术网

C# asp从具有服务器路径的目录中选择随机文件

C# asp从具有服务器路径的目录中选择随机文件,c#,asp.net,C#,Asp.net,我找到了解决问题的方法,但我不知道正确的方法是什么 我使用这些路径,但可能没有一条有效: "/Images/defaultImages" "~/Images/defaultImages" "Images/defaultImages" 正确的路径是什么?试试看 Server.MapPath("~/Images/defaultImages") 还要确保您的defaultImages文件夹包含图像文件不要重新创建Random,请执行一次:private static Random R=new R

我找到了解决问题的方法,但我不知道正确的方法是什么

我使用这些路径,但可能没有一条有效:

"/Images/defaultImages"
"~/Images/defaultImages"
"Images/defaultImages"
正确的路径是什么?

试试看

Server.MapPath("~/Images/defaultImages")

还要确保您的
defaultImages
文件夹包含图像文件

不要重新创建
Random
,请执行一次:
private static Random R=new Random()
是否收到任何错误/异常?否,但所有路径返回null。
私有静态只读
命名空间
“MapPath”
不存在于命名空间
“Microsoft.SqlServer.Server”
Server.MapPath属于页面命名空间。请尝试
Page.Server.MapPath
HttpContext.Current.Server.MapPath
@rezayegane:
Server.MapPath中的
服务器
在页面或控件中可用。如果代码在web上下文之外,则可以使用
HttpContext.Current.Server.MapPath
。如果您在web上下文中但存在名称冲突,则可以使用
this.Server.MapPath
指定您使用的是属性,而不是
Microsoft.SqlServer.Server
类。是否确实在defaultImages文件夹中有文件该方法返回
null
的方式只有两种;当您发送的路径为空或
null
时,或者当您收到异常时。由于该方法捕获异常并以静默方式忽略它,因此找出错误的唯一方法是调试代码。捕获异常并忽略它们是非常糟糕的,您应该要么处理异常,要么根本不捕获它。如果从方法中删除
try….catch
,您将看到所得到的错误。
Server.MapPath("~/Images/defaultImages")