【问题标题】:how to get path of the file to be deleted from the server如何获取要从服务器中删除的文件的路径
【发布时间】:2010-02-10 10:54:40
【问题描述】:

我想删除服务器目录中存在的文件。我尝试过以下代码,但 file.exist 中的代码永远不会运行。它总是跳过它,告诉我文件不存在。但文件存在。可以smone请帮助我。提前感谢

string filename = "Template\\copy.jpg";
        if(System.IO.File.Exists(filename))
        {
            System.IO.File.Delete(filename);
        }

【问题讨论】:

    标签: c# .net asp.net file-io


    【解决方案1】:

    试试

    string fileName = Server.MapPath(@"/Template/copy.jpg");
    

    【讨论】:

      【解决方案2】:

      您可以使用Server.MapPath 获取路径。例如

      string filename = Server.MapPath("~/Template/copy.jpg");
      System.IO.File.Delete(filename);
      

      【讨论】:

      • 您可能需要在“~/”路径上调用 ResolveUrl,然后再将其传递给 Server.MapPath。
      猜你喜欢
      • 2017-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多