views:

78

answers:

4

Group,

I'm looking for new and fun ways to write an array of strings to a .txt file. -This file will be rewritten every time its called and saved. -The file name will be dynamic enough that the .exe will know what string goes where.

For example:

 ~/someFile.exe "fileName" "string|string|string|"

-In this example someFile.exe is called to write strings to this "fileName".

Any suggestions?

Chad

A: 

If this isnt the exact call your looking for, it will be in the system.io.file namespace.

http://msdn.microsoft.com/en-us/library/system.io.file.writealllines.aspx

In your exe you'll need to break the string into an array based on the seperator before calling this, string.Split() will do the job.

http://msdn.microsoft.com/en-us/library/system.string_methods.aspx

asawyer
Thanks...the links are a grate help. ---I'm trying to think out side of the microsoft box on this.
Chad Sellers
Ok, but that might be difficult as your limited to using framework objects / methods, unless you say, write some file IO in some other object and import it in as unmanaged code and use that.
asawyer
+5  A: 

You could use the File.WriteAllLines method:

public class Program
{
    static void Main(string[] args)
    {
        File.WriteAllLines(
            args[0], 
            args[1].Split(new [] { '|' }, StringSplitOptions.RemoveEmptyEntries)
        );
    }
}

And then call: someFile.exe "fileName" "string|string|string|"

Darin Dimitrov
Thanks...can this way work for any Nth amount of string args.
Chad Sellers
No, because this program supposes that two arguments are passed: the first represents the filename and the second represents a `|` separated list of strings. If you pass less than two parameters the program will crash and if pass more than two it will ignore everything after the second argument.
Darin Dimitrov
O ok...Darin..what about giving the split args[1] object values and place each string in a dynamic order. --For example: "StringOne|StringTwo|StringThree" --Output Text File: StringTwo StringOne StringThree
Chad Sellers
@Chad did you try the example I provided?
Darin Dimitrov
I'm trying to fit this in with what i have below:========= public static void Open(string fileName,string arrInput) { string FILE_NAME = fileName + ".txt"; if (File.Exists(FILE_NAME)) {Console.WriteLine("{0} already exists.", FILE_NAME); File.Delete(FILE_NAME); } using (StreamWriter sw = File.CreateText(FILE_NAME)) { sw.WriteLine("{0}", arrInput); sw.Close(); } Console.WriteLine("arrInput {0} :",arrInput.ToString()); Console.ReadKey(true); }
Chad Sellers
And what's the problem?
Darin Dimitrov
The last string is the only string being written and not the other two .
Chad Sellers
A: 

I'd just loop through it and call StreamWriter.WriteLine.
Here's a sample showing how to do it: Writing Text to a File

ho1
A: 

Can you explain what you mean by "The file name will be dynamic enough that the .exe will know what string goes where"?

Also, you say you want new and "fun" ways. So what are the old and boring ones you've already thought of? :-)

David
I wondered about this as well.
asawyer