Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

C# Tutorial: Writing Text Files

streamwriter

  • Please log in to reply
37 replies to this topic

#13 PlayaSkater

PlayaSkater

    CC Resident

  • Just Joined
  • PipPipPipPip
  • 52 posts

Posted 17 August 2008 - 12:53 PM

Thanks for the tutorial, Xav. I haven't gotten too much into file interaction with C# and this got me interested. By the way, would this be an appropriate method for storing passwords of some sort? Like if you were making a login program, it would simply read for the Username and corresponding password?
  • 0

#14 Xav

Xav

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 8356 posts

Posted 17 August 2008 - 12:59 PM

You could certainly do that, although if you have lots of users then a database or XML would be better. Whatever you use, remember to encrypt the file containing the data, for security.

And you're welcome. :)
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#15 amrosama

amrosama

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 2765 posts

Posted 24 August 2008 - 04:14 PM

hi xav, im glad u brought the System.IO class cuz it reminded me with a little coding problem that i didnt find a reason or solution for it heres the code:
 System.IO.FileInfo MyFile = new System.IO.FileInfo("newfile.txt");
 MyFile.Create();
and any statement that uses the same files gives an "file still opened by another process" exception.
and i didnt find any ".close()" method ..
  • 0
yo homie i heard you like one-line codes so i put a one line code that evals a decrypted one line code that prints "i love one line codes"
eval(base64_decode("cHJpbnQgJ2kgbG92ZSBvbmUtbGluZSBjb2Rlcyc7"));
www.amrosama.com | the unholy methods of javascript

#16 Xav

Xav

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 8356 posts

Posted 25 August 2008 - 09:50 AM

I've never actually used a FileInfo object. How about:
//Create a new file called newfile.txt.
System.IO.File.Create("newfile.txt");

Notice how no variables are necessary, as File.Create() is static.
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#17 amrosama

amrosama

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 2765 posts

Posted 25 August 2008 - 09:57 AM

still, i have the same problem when i try to access the same file like this
  System.IO.File.Create("newfile.txt");
  System.IO.File.ReadAllText("newfile.txt");
it gives the same exception on line 2
  • 0
yo homie i heard you like one-line codes so i put a one line code that evals a decrypted one line code that prints "i love one line codes"
eval(base64_decode("cHJpbnQgJ2kgbG92ZSBvbmUtbGluZSBjb2Rlcyc7"));
www.amrosama.com | the unholy methods of javascript

#18 Xav

Xav

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 8356 posts

Posted 25 August 2008 - 10:12 AM

Eh? How are you going to read the text from a file if you've just created it? It's going to be blank!
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#19 amrosama

amrosama

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 2765 posts

Posted 25 August 2008 - 10:19 AM

still, it returns "being used by another process" exception....but never mind
i found an alternate by using a stream writer
  System.IO.StreamWriter create = new System.IO.StreamWriter("newfile.txt");
  create.Close();

  • 0
yo homie i heard you like one-line codes so i put a one line code that evals a decrypted one line code that prints "i love one line codes"
eval(base64_decode("cHJpbnQgJ2kgbG92ZSBvbmUtbGluZSBjb2Rlcyc7"));
www.amrosama.com | the unholy methods of javascript

#20 Xav

Xav

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 8356 posts

Posted 25 August 2008 - 10:28 AM

That's the same method I used in my tutorial! :D
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#21 amrosama

amrosama

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 2765 posts

Posted 25 August 2008 - 10:30 AM

ohh right:D sorry dude
  • 0
yo homie i heard you like one-line codes so i put a one line code that evals a decrypted one line code that prints "i love one line codes"
eval(base64_decode("cHJpbnQgJ2kgbG92ZSBvbmUtbGluZSBjb2Rlcyc7"));
www.amrosama.com | the unholy methods of javascript

#22 Xav

Xav

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 8356 posts

Posted 25 August 2008 - 10:32 AM

He he lol it's alright. It would be a good idea to call Dispose() after you call Close(), just to be on the safe side.
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#23 PlayaSkater

PlayaSkater

    CC Resident

  • Just Joined
  • PipPipPipPip
  • 52 posts

Posted 26 August 2008 - 04:55 PM

Hey, Xav, let's say I wanted the console to pop up and I would enter the text myself to be written to the text file. Here's the code I have, but it doesn't seem to be working. The console leaves immediately:

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

public class writetext
{
    static void Main()
    {
        //Ask for text to write to buffer
        Console.WriteLine("What would you like to write to text file?  ");
        string buffer = Console.ReadLine();
       

        //Write buffer to textfile
        string path = @"C:/Users/MyName/Documents/C# file.txt";
        StreamWriter txtwrite = new StreamWriter(path);
        txtwrite.WriteLine(buffer);
        txtwrite.Close();
    }
}

  • 0

#24 Xav

Xav

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 8356 posts

Posted 27 August 2008 - 05:47 AM

What do you mean by "leaves immediately"? Where does it go? Is the file still written?
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?





Also tagged with one or more of these keywords: streamwriter

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download