views:

243

answers:

2

Hi friends,

I have developed a web application in visual studio 2008

its in asp.net c#.

i have user a html page and one txt file in that application

but both are not inside my application

bot are outside

so when i try to run the same application in another system i will get error because of that files.

i want to include that files inside that application.

below is the code lines which i used to write and read that files

//file write test.txt FileStream file1 = new FileStream("test.txt", FileMode.Create, FileAccess.Write);

        // Create a new stream to write to the file
        StreamWriter sw1 = new StreamWriter(file1);

        // Write a string to the file
        sw1.Write(emailId);

        // Close StreamWriter
        sw1.Close();

        // Close file
        file1.Close();

// * Write to file *

        // Specify file, instructions, and privelegdes
        FileStream file = new FileStream("test.html", FileMode.Create, FileAccess.Write);

        // Create a new stream to write to the file
        StreamWriter sw = new StreamWriter(file);

        // Write a string to the file
        sw.Write(BodyLiteral.Text);

        // Close StreamWriter
        sw.Close();

        // Close file
        file.Close();

// * Read from file *

        // Specify file, instructions, and privelegdes
        file = new FileStream("test.txt", FileMode.OpenOrCreate, FileAccess.Read);

        // Create a new stream to read from a file
        StreamReader sr = new StreamReader(file);

        // Read contents of file into a string
        string cval = sr.ReadToEnd();
        Response.Write(cval);
        // Close StreamReader
        sr.Close();

        // Close file
        file.Close();

//html file reading

string text = File.ReadAllText(@"D:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\test.html");

actually now my both file is in

D:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\

i want replace all these codes to file inside my web application.

please help me to solve this.

Thanks

A: 

Best thing to do is to add the files to your solution.

In solution exploere you can right-click and add an existing item. Change your code to read from that location, so when you deploy your code it will be in a known location and part of the deployment.

Oded
A: 

Your best bet would be Server.MapPath().

Example:

Put the files inside folder "file" (you can make a folder in your solution, by right clicking your solution and choose add folder).. then right click the folder..choose existing item , and then choose your files..

To make the path to your files local.. use the follow

Server.MapPath("~\\files\\test.html");

Your code modified

 FileStream file = new FileStream(  Server.MapPath("~\\files\\test.html"), FileMode.Create, FileAccess.Write);
Madi D.