简单问题,解决给分,也许你未曾遇到过的问题

时间:2022-03-13 19:54:58
DirectoryInfo DiFile=new DirectoryInfo(@Server.Mappath("/image/upload"))
如上,在使用Server.Mappath的时候出错,因为要求DirectoryInfo的参数是这样的,例如C:\\Windows\\System而Server.Mappath得到的却是C:\Window\System使用转义字符后仍然不行,除非直接输入@"C:\Windows\System"做为DirectoryInfo 的参数才可以,另MSDN中提到在VB中却要求是C:\Windows\System这样的,在C#中是C:\\Windows\\System这样的,请问,我想用C#解决,怎么办?
MSDN中的URL:lp://MS.VSCC.v80/MS.MSDN.v80/MS.NETDEVFX.v20.chs/cpref8/html/T_System_IO_DirectoryInfo.htm
谢谢

7 个解决方案

#1


直接Server.Mappath("/image/upload"))
不要加上@
做为字符串传进去会自动辨识,不会认成转义字符

#2


Server.Mappath("./image/upload"))

#3


DirectoryInfo DiFile=new DirectoryInfo(Server.Mappath("~/image/upload"))

DirectoryInfo DiFile=new DirectoryInfo(Server.Mappath("image/upload"))

我确定上面两个在服务器不禁Server.Mappath()方法的情况下没有任何问题。

在禁Server.Mappath()方法的时候要Request.PhysicalApplicationPath属性来自己转化

LZ可以自己测试一下


#4


我试了下还是不行,因为我在Server.Mappath(str)中使用了变量,str是临时得到的,处理后仍然不行,

#5


DirectoryInfo unhost = new DirectoryInfo(Server.MapPath(Newpath + "/Pic"));
FileInfo[] FileDns = unhost.GetFiles("*.jpg");
其中Newpath="image/upload";
这个程序总是有问题,怎么搞?

#6


TO POPO_POPO(执法者) 
你说的的确对,前提是Server.Mappath()中的参数是字符串,若改成已知变量就不行了,如str="image/upload";
DirectoryInfo unhost = new DirectoryInfo(Server.MapPath("image/upload"))这样没有问题;
若是
DirectoryInfo unhost = new DirectoryInfo(Server.MapPath(str))就出问题了,请指教.

#7


try:

DirectoryInfo DiFile=new DirectoryInfo(Server.Mappath(@"/image/upload"))

#1


直接Server.Mappath("/image/upload"))
不要加上@
做为字符串传进去会自动辨识,不会认成转义字符

#2


Server.Mappath("./image/upload"))

#3


DirectoryInfo DiFile=new DirectoryInfo(Server.Mappath("~/image/upload"))

DirectoryInfo DiFile=new DirectoryInfo(Server.Mappath("image/upload"))

我确定上面两个在服务器不禁Server.Mappath()方法的情况下没有任何问题。

在禁Server.Mappath()方法的时候要Request.PhysicalApplicationPath属性来自己转化

LZ可以自己测试一下


#4


我试了下还是不行,因为我在Server.Mappath(str)中使用了变量,str是临时得到的,处理后仍然不行,

#5


DirectoryInfo unhost = new DirectoryInfo(Server.MapPath(Newpath + "/Pic"));
FileInfo[] FileDns = unhost.GetFiles("*.jpg");
其中Newpath="image/upload";
这个程序总是有问题,怎么搞?

#6


TO POPO_POPO(执法者) 
你说的的确对,前提是Server.Mappath()中的参数是字符串,若改成已知变量就不行了,如str="image/upload";
DirectoryInfo unhost = new DirectoryInfo(Server.MapPath("image/upload"))这样没有问题;
若是
DirectoryInfo unhost = new DirectoryInfo(Server.MapPath(str))就出问题了,请指教.

#7


try:

DirectoryInfo DiFile=new DirectoryInfo(Server.Mappath(@"/image/upload"))