如何使用asp.net读取或写入文本文件 [英] how to read or write text file using asp.net

查看:61
本文介绍了如何使用asp.net读取或写入文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行代码时如何使用asp.net读取或写入文本文件,但出现错误:-

When i run the code how to read or write text file using asp.net but it gives an error:--

protected void Button3_Click(object sender, EventArgs e)
    {
        string path = FileUpload1.PostedFile.FileName;
        if(!string.IsNullOrEmpty(path))
        {
            string[] readText=File.ReadAllLines(path);
            StringBuilder strbuild=new StringBuilder();
            foreach(string s in readText)
            {
                strbuild.Append(s);
                strbuild.AppendLine();
                   
            }
            TextBox2.Text=strbuild.ToString();
        }
        
    }


像这样:


like this:

Could not find file 'C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\aaaaaa.txt'.

但是实际路径不正确,因此将文本文件放在它显示的桌面和aaaaaa.txt文件显示在不同的路径中,请对此提供帮助

因此,我该如何解决此问题?

but the actuall path is not right the text file is put on the desktop it shows and the aaaaaa.txt file is shown in the diffenent path please help me on this

so how can i solve this problem

推荐答案

请记住,ASP.NET应用程序在IIS的虚拟文件夹和沙箱中运行.该应用程序必须有权访问您尝试加载的任何文件夹/文件.您的桌面位置将与其他用户不同,并且IIS_USR帐户将无法访问您的桌面位置.您似乎正在通过IDE从文件系统运行该应用程序,因此它将在该路径中查找文件.

您应该在应用程序结构中创建一个文件夹,并使用Server.MapPath
打开任何文件.
另外,请使用您在其中输入问题的文本区域上方的代码"工具栏项来格式化您发布的所有代码段.
Remember that ASP.NET applicataions operate within a virtual folder and sandbox in IIS. The application must have access to any folderes/files you are attempting to load. Your desktop location will not be the same as another users and will not be accessible by the IIS_USR account regardless. You appear to be running the application from the file system via the IDE so it looking for the file in that path.

You should create a folder within your application structure and open any files using Server.MapPath

Also, please format any code snippets you post using the "code" toolbar item above the textarea you entered your question in.


您遇到的问题是文件名,实际上是文件的位置.使用ASP.NET,您将无法访问站点根目录所定义的子目录之外的任何文件.

—SA
You problem is the file name, actually the location of the file. With ASP.NET, you cannot access any file outside the sub-directory defined by the root directory of your site.

—SA


您也可以在应用程序文件夹之外读取或写入文件,但是您将拥有该文件夹的全部权利.
并在web.config文件中定义该路径,并使用streamReader和Streamwriter.
You can read or write the file out side the application folder as well but you will have the ful right on that folder.
and define that path in web.config file and use the streamReader and Streamwriter.


这篇关于如何使用asp.net读取或写入文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆