how to create Text file on server by ASP.net ?

SMortezaa

New Member
Hi<br />
<br />
I have write this code and it is working correctly on the Local host but when i upload it to website it give error that file cannot found on server. <br />
<br />
protected void Button1_Click1(object sender, EventArgs e)<br />
{<br />
if (!File.Exists(Server.MapPath(@"App_data\TextFiles\Text1.txt")))<br />
{<br />
File.Create(Server.MapPath(@"App_data\TextFiles\Text1.txt"));<br />
Label1.Text = "File Created";<br />
}<br />
else<br />
Label1.Text = "File Already Created";<br />
}<br />
 

MilindK

New Member
There is no errors in your code it may be the problem of your hosting environment,

Regards,
Milind Kansagara
Web Developer ( Cybercomcreation )
Ahmedabad.
http//milindkansagara1984.blogspot.com
 

IronyMan

New Member
That means the path in local and the path in server is not the same.

You must use the correct path by using the Server.MapPath()

EXAMPLE BELOW


Const ForWriting = 2

sCustomer= Request("Customer")

sIPaddress = Request("IPaddress")

sTemp1 = Request("Temp1")

Dim objFSO 'FileSystemObject Variable
Dim objFile 'File Object Variable

Dim strFile As String

Dim sAddressFile As String = sCustomer &".txt"
strFile = Server.MapPath(sAddressFile)

objFSO = CreateObject("Scripting.FileSystemObject")
objFile = objFSO.OpenTextFile(strFile, ForWriting, True)

objFile.WriteLine(sTemp1)

objFile.Close()
 
Top