Reading and writing files inside asp.net web application

I developed an ASP.NET web application in Visual Studio 2008.

I have an HTML document and one text file in the application, but both of them are not included in my application.

Both are outside, so when I try to run the same application on a different system, I get an error because I miss these files.

I want to include files in the application when I deploy it.

Below is the code that I use to read and write 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");

Both of my files are located in: D: \ Program Files \ Microsoft Visual Studio 9.0 \ Common7 \ IDE \

How can I deploy these two files with the application so that the program runs on a different system?

+3
2

Server.MapPath().

:

"" ( , ). . , .

,

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

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

.

. , .

+1

Source: https://habr.com/ru/post/1725942/