检查ASP.NET中的服务器中是否存在文件

Pat*_*ins 8 javascript asp.net file c#-2.0

        string jSFile = ResolveUrl("~/MyProject/JavaScripts/dir/test.js");
        if (!System.IO.File.Exists(jSFile))
        {
           ...
        }
Run Code Online (Sandbox Code Playgroud)

这段代码不起作用,我想这是jSFile与IO.File.Exists不能很好地工作,但我知道jSFile有一个有效的路径因为我以后用了几行

Page.ClientScript.RegisterClientScriptInclude("myfile",jSFile); 
Run Code Online (Sandbox Code Playgroud)

它确实将JavaScript文件附加到ASPX,一切正常.

知道如何检查文件是否存在的任何想法?

Sky*_*ers 16

if (!System.IO.File.Exists(Server.MapPath(jSFile)))
Run Code Online (Sandbox Code Playgroud)

  • HttpContext.Current.Server.MapPath(); (4认同)