Click here to Skip to main content
15,907,396 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When i run the code how to read or write text file using asp.net but it gives an error:--
C#
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'.
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
Posted
Updated 4-Oct-11 16:13pm
v3

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.
 
Share this answer
 
v2
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
 
Share this answer
 
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.
 
Share this answer
 
 
Share this answer
 
v2
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900