开发者

C#- What is the best way for write text to a file from array of strings

开发者 https://www.devze.com 2023-01-15 10:19 出处:网络
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.

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 wil开发者_如何学Gol 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


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


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


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号