|
-
Apr 2nd, 2004, 05:17 AM
#1
Thread Starter
Frenzied Member
Writing to a txt-file.
I wish to save information from a simple textbox to a txt-file. How do I do that?
-
Apr 2nd, 2004, 07:35 AM
#2
At the very top of your webform, above everything else in the code window, place this line:
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
-
Apr 2nd, 2004, 07:59 AM
#3
Thread Starter
Frenzied Member
Thanks, but what if the file doesn't already exist?
-
Apr 2nd, 2004, 08:04 AM
#4
If the file specified by path does not exist, it is created.
-
Apr 2nd, 2004, 12:26 PM
#5
Thread Starter
Frenzied Member
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.
-
Apr 6th, 2004, 08:02 AM
#6
Frenzied Member
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
-
Apr 14th, 2004, 08:55 AM
#7
Thread Starter
Frenzied Member
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?
-
Apr 15th, 2004, 09:07 AM
#8
Frenzied Member
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.
Magiaus
If I helped give me some points.
-
Apr 15th, 2004, 09:09 AM
#9
Frenzied Member
what content type does the framework support btw?
Magiaus
If I helped give me some points.
-
Apr 15th, 2004, 09:24 AM
#10
Thread Starter
Frenzied Member
The same thing happens if I open the txt-file with Word or any other editor than Notepad
-
Apr 16th, 2004, 08:23 AM
#11
Thread Starter
Frenzied Member
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????
-
Apr 16th, 2004, 08:26 AM
#12
Thread Starter
Frenzied Member
Sorry Magius, I'm not sure I understand your last reply?
-
Apr 16th, 2004, 11:34 AM
#13
Frenzied Member
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?
Magiaus
If I helped give me some points.
-
Apr 16th, 2004, 11:36 AM
#14
Frenzied Member
Code:
System.IO.TextWriter txtWr = new System.IO.TextWriter();
txtWr.Encoding = System.Text.Encoding.Unicode;
except you would set it with your TextWrter that is setup for your file.
Magiaus
If I helped give me some points.
-
Apr 16th, 2004, 02:15 PM
#15
Thread Starter
Frenzied Member
Thanks a lot Magius I think that would solve the problem. But have you seen this? I don't understand why.
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????
-
Apr 16th, 2004, 04:44 PM
#16
Frenzied Member
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.
Magiaus
If I helped give me some points.
-
Apr 17th, 2004, 01:21 PM
#17
Thread Starter
Frenzied Member
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?
-
Apr 17th, 2004, 02:12 PM
#18
Frenzied Member
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.
Magiaus
If I helped give me some points.
-
Apr 18th, 2004, 04:05 PM
#19
Frenzied Member
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
-
Apr 19th, 2004, 01:07 AM
#20
Thread Starter
Frenzied Member
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 ;-)
-
Apr 19th, 2004, 12:27 PM
#21
Frenzied Member
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.
Magiaus
If I helped give me some points.
-
Apr 20th, 2004, 02:59 AM
#22
Thread Starter
Frenzied Member
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
-
Apr 21st, 2004, 01:40 AM
#23
Thread Starter
Frenzied Member
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?
-
Apr 21st, 2004, 01:50 AM
#24
Frenzied Member
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.
Magiaus
If I helped give me some points.
-
Apr 21st, 2004, 01:51 AM
#25
Thread Starter
Frenzied Member
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")
....
-
Apr 21st, 2004, 02:04 AM
#26
Frenzied Member
Code:
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();
}
}
}
that code works
Magiaus
If I helped give me some points.
-
Apr 21st, 2004, 02:05 AM
#27
Thread Starter
Frenzied Member
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)
-
Apr 21st, 2004, 02:06 AM
#28
Frenzied Member
Magiaus
If I helped give me some points.
-
Apr 21st, 2004, 02:14 AM
#29
Thread Starter
Frenzied Member
Thanks, I will take a look at your code, but I'm not a C# specialist, (yet ;-))
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|