Results 1 to 29 of 29

Thread: Writing to a txt-file.

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Denmark
    Posts
    1,049

    Question Writing to a txt-file.

    I wish to save information from a simple textbox to a txt-file. How do I do that?

  2. #2
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    At the very top of your webform, above everything else in the code window, place this line:

    VB Code:
    1. Imports System.IO

    Then you can use this code:

    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, _
    2. ByVal e As System.EventArgs) Handles Button1.Click
    3.     Dim swFileText as StreamWriter
    4.    
    5.     swFileText = File.createtext("C:\TESTFILE.txt")
    6.     swFileText.Write (textbox1.Text)
    7.     swFileText.Close
    8. End Sub

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Denmark
    Posts
    1,049
    Thanks, but what if the file doesn't already exist?

  4. #4
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    If the file specified by path does not exist, it is created.

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Denmark
    Posts
    1,049
    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.

  6. #6
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602
    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

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Denmark
    Posts
    1,049
    This is really strange. When I write the danish letters æ.ø,å to the txt-file and opens with Notepad, the letters are displayed correctly.
    Code:
    Spørgsmål eller problemer: <a href=mailto:[email protected]>Klik her</a>
    But if I open the file with Excel (or if I save the file as xls) the danish letters are displayed like this:
    Code:
    SpørgsmÃ¥l eller problemer: <a href=mailto:[email protected]>Klik her</a>
    How can I prevent this?

  8. #8
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267
    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.

  9. #9
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267
    what content type does the framework support btw?
    Magiaus

    If I helped give me some points.

  10. #10

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Denmark
    Posts
    1,049
    The same thing happens if I open the txt-file with Word or any other editor than Notepad

  11. #11

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Denmark
    Posts
    1,049
    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????

  12. #12

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Denmark
    Posts
    1,049
    Sorry Magius, I'm not sure I understand your last reply?

  13. #13
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267
    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.

  14. #14
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267
    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.

  15. #15

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Denmark
    Posts
    1,049
    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????

  16. #16
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267
    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.

  17. #17

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Denmark
    Posts
    1,049
    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?

  18. #18
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267
    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.

  19. #19
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602
    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

  20. #20

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Denmark
    Posts
    1,049
    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 ;-)

  21. #21
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267
    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.

  22. #22

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Denmark
    Posts
    1,049
    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

  23. #23

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Denmark
    Posts
    1,049
    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?

  24. #24
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267
    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.

  25. #25

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Denmark
    Posts
    1,049
    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")
    ....

  26. #26
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267
    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.

  27. #27

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Denmark
    Posts
    1,049
    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)

  28. #28
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267
    don't know
    Magiaus

    If I helped give me some points.

  29. #29

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Denmark
    Posts
    1,049
    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
  •  



Click Here to Expand Forum to Full Width