When saving file - where is it saved ?

M

Mr. X.

Helo,

For the code (The code is the code-behind an aspx page) :
'**************************************************

Dim fileName As String

Dim st As System.IO.StreamWriter

filename = "a.txt"

st = New System.IO.StreamWriter(fileName)

st.Write("this is a text")

st.Close()

st = Nothing

'**************************************

The above code runs, there is not any file created.

How can I save (and load) a file, and just putting simple text on it ?

Thanks :)
 
N

Nathan Sokalski

I believe it is created in the current directory. However, it is bad
programming practice to give just a filename. Try something like the
following:


Private Sub btnCreateText_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnCreateText.Click
'CreateText either creates or overwrites
Dim texttestwriter As StreamWriter =
File.CreateText(Server.MapPath("TextTest.txt"))
Dim writetime As Date = Date.Now
texttestwriter.WriteLine("This line was written at {0} {1}",
writetime.ToShortDateString(), writetime.ToLongTimeString())
texttestwriter.Close()
End Sub

Private Sub btnAppendText_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnAppendText.Click
'AppendText either creates or appends
Dim texttestwriter As StreamWriter =
File.AppendText(Server.MapPath("TextTest.txt"))
Dim writetime As Date = Date.Now
texttestwriter.WriteLine("This line was written at {0} {1}",
writetime.ToShortDateString(), writetime.ToLongTimeString())
texttestwriter.Close()
End Sub


Notice that when declaring and creating my StreamWriters I do not use the
StreamWriter constructor and I also use the Server.MapPath(filename)
function. If you have any more questions, feel free to ask. Good Luck!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,278
Latest member
BuzzDefenderpro

Latest Threads

Top