访问服务器上的文件(asp.net、webmatrix)



我想读取一个*.txt文件并编辑这个文件。我使用Webmatrix,它在我的电脑上运行。但在将其发布到服务器(Web部署)后,它就不再工作了。

string transmission;
string path;
path = "E:\Documents\My Web Sites\Trackercontrol v2\backup.txt";
public int insert()
{
    using (StreamReader sr = File.OpenText(path)) 
    {
        while ((transmission = sr.ReadLine()) != null) 
        {
            // etc.
        }
    }
}

我发布了这篇文章,调整了.txt文件的路径,但try/cath方法告诉我,对该路径的访问被拒绝。我认为读出文件不是问题,但编辑或清除文件会造成这个问题。

我该怎么解决这个问题?非常感谢!

您提供了本地驱动器的路径,该路径可能在服务器上存在,也肯定不存在。首先将文件上传到服务器上,然后读取。您可以获得的正确路径

 string path;
 path = Server.MapPath("~/filename.txt"); // Considered file is placed at Root of your site

检查应用程序池在哪个帐户下运行?请确保此帐户对相关文件具有读/写访问权限。

此外,使用也是个好主意

var path = Server.MapPath(relativePathToFile);

最新更新