I wish to save information from a simple textbox to a txt-file. How do I do that?
Printable View
I wish to save information from a simple textbox to a txt-file. How do I do that?
At the very top of your webform, above everything else in the code window, place this line:
VB Code:
Imports System.IO
Then you can use this code:
VB Code:
Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click Dim swFileText as StreamWriter swFileText = File.createtext("C:\TESTFILE.txt") swFileText.Write (textbox1.Text) swFileText.Close End Sub
Thanks, but what if the file doesn't already exist?
Quote:
If the file specified by path does not exist, it is created.
Great, great great, just what I needed. Is it also possible to write to a nonexisting Excel-file? I'm working on a financial system, and everything is handled with Excel.
if you want the data to your client in a special format please use the response streams and set content type to whatever format you like... I recomment a search on the forum...
kind regards
Henrik
This is really strange. When I write the danish letters æ.ø,å to the txt-file and opens with Notepad, the letters are displayed correctly.
But if I open the file with Excel (or if I save the file as xls) the danish letters are displayed like this:
How can I prevent this?
Your just writing text and saving it with an xsl extention? I bet the problem is excel has a file format. You know like an avi, bit, acces db and so on the information in the file is encoded in a special format with some type of file header info at the top/begining and that is read and it provides information needed to read the file. Like a swf(flash movie) it's header defines info on the file size, number of images in the movie, password for the movie if it is looked from importing, and the byte offsets to use when reading the structs that define all the data. It also tells you if the rest of the data is compressed with zlib or not..... in iorder to make a flash movie from a program you make you have to write the info out in this format. You see what I'm talking about?
and sorry I know jack about excel because I find it next to useless. Which translates to I never need it so I haven't learned what it can really do, yet.
what content type does the framework support btw?
The same thing happens if I open the txt-file with Word or any other editor than Notepad
Well the writing to a file with extension "C:\testfile.txt" is working fine as long as I use Localhost. But nothing happens when I deploy to my web server????
Sorry Magius, I'm not sure I understand your last reply?
it was directed at someone else.
I have a question for you though do you have the encoding set to unicode? I'm not sure exactly where it is set but that might fix the problem. Which writer/stream are you using?
except you would set it with your TextWrter that is setup for your file.Code:System.IO.TextWriter txtWr = new System.IO.TextWriter();
txtWr.Encoding = System.Text.Encoding.Unicode;
Thanks a lot Magius I think that would solve the problem. But have you seen this? I don't understand why.
Quote:
Well the writing to a file with extension "C:\testfile.txt" is working fine as long as I use Localhost. But nothing happens when I deploy to my web server????
most likely ASP.Net need to have permisions added on the folder your writing to. I had the same trouble not long ago. I was used to only having to set it in IIS but for .net you actual have to make sure the ASP.Net windows account has write permision on the folder.
OK, good to hear I'm not the only one having that problem. Where do I find the permissions and change them? Is it in the server settings or can I write some code in the web.config file?:confused:
I think you have to actual do it or have it done on the server. It would be nice to do it in the web congig but if you can I haven't heard of it.
I had some problems with swedish letters like åöä and I had to set the textwriter encoding properly.. it is one of the parameters.. check in msnd, I don't have dev studio in front of me.. :(
kind regards
Henrik
To Magius: do you know exactly what to be done on the server? It's not my own server, so I have to know exactly what to be done, because I can't make experiments myself :-(. Sorry for disturbing you all the time ;-)
For a windows 2000 server the admin would navigate to the folder right click selcet proprties. In the properties dialog select security add ASP.NET and grant ASP.NET Write permisions or Full control.
For XP I dunno, the new security tab is weird.
Great, thanks a lot. I think it's a Win2000 server.
I think it's strange, that no error occurs when writing to the file and nothing happens. Normally you are told that you don't have permission to do this and that.
I will contact the administrator and hope that will be it
It is a Windows 2000 server and now I've have done the things you told me to and I have also set "Everyone" to have full control, but still nothing happens. Are you sure you didn't made other changes for writing to the txt-file?
the only other thing i did was check the write box in IIS Console->The Site->The Folder->Right Click->Properties->Directory Tab->Check the Write box->Apply.
If you have the ASP.NET name ServerName\ASPNET(no dot) set for full control on the folder you want to write files to, and you have write enabled in IIS it should work. If it doesn't look for a new host flarehosting.com 9.95 a month.
I just upload mine to flare and it wrote fine. I have a trace class that writes custom logs for me using System.IO.Stream.
Or maybe it's better if I write the file to the current server (if the permissions are set to Full Control this must be possible). With databases the current path is server.mappath, but how do I write the code for saving on the server instead of this:
Code:Dim swFileText As StreamWriter
swFileText = File.CreateText("c:\\" & Parametre(0) & "_" & Parametre(1) & "_" & Date.Now.ToShortDateString & "_" & time & minut & sekund & ".txt")
....
that code worksCode:internal class TraceLite
{
/// <summary>Get the path to the log File folder.</summary>
private static string LogFolder{get{return System.Web.HttpContext.Current.Server.MapPath("log");}}
/// <summary>Open a stream to log file.</summary>
/// <param name="log"></param>
/// <returns>System.IO.Stream</returns>
private static System.IO.Stream OpenLog(string log)
{
if(System.IO.File.Exists(LogFolder + "\\" + log + ".log"))
{
return System.IO.File.Open(LogFolder + "\\" + log + ".log", System.IO.FileMode.Append, System.IO.FileAccess.Write);
}
else
{
return System.IO.File.Open(LogFolder + "\\" + log + ".log", System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write);
}
}
/// <summary>Write to the common log file.</summary>
/// <param name="text">The text to be written.</param>
public static void Write(string text)
{
System.IO.Stream stream = OpenLog("common");
if(stream != null)
{
byte[] b = System.Text.Encoding.ASCII.GetBytes(text);
stream.Write(b, 0, b.Length);
stream.Close();
}
}
/// <summary>Write a line of text to the common log file.</summary>
/// <param name="text">Text to be written.</param>
public static void WriteLine(string text)
{
System.IO.Stream stream = OpenLog("common");
if(stream != null)
{
byte[] b = System.Text.Encoding.ASCII.GetBytes(text + "\r\n");
stream.Write(b, 0, b.Length);
stream.Close();
}
}
}
Yes it's very strange, I have done all the things as you have mentioned. I have given full control to Everyone and the ASPNET setting: aspnet_wp account (GEARLOES\ASPNET)
don't know
Thanks, I will take a look at your code, but I'm not a C# specialist, (yet ;-))