3

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

4 Answers 4

7

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|"

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks...can this way work for any Nth amount of string args.
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.
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 did you try the example I provided?
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); }
|
0

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

2 Comments

Thanks...the links are a grate help. ---I'm trying to think out side of the microsoft box on this.
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.
0

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

Comments

0

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? :-)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.