Solved

String saved to a .txt file

Posted on 2009-07-06
8
308 Views
Last Modified: 2012-06-27
I currently have a string array that has data in each section as a comma sepparated list.

I need to pull this data into a txt file for later use.

how is this done.
0
Comment
Question by:cotj73
8 Comments
 
LVL 40

Accepted Solution

by:
mrjoltcola earned 100 total points
Comment Utility
// This might be wrong, I am writing this quick before heading out.


// Something like this
using System.IO;


StreamWriter file = new StreamWriter("out.txt");

string [] strings = {"red", "blue"};

foreach(String s in strings) {
   file.WriteLine(s);
}
0
 
LVL 1

Author Comment

by:cotj73
Comment Utility
I tried this, but when i look at the file it is blank.
Also how do i determine where this out.txt will be saved
0
 
LVL 55

Expert Comment

by:Jaime Olivares
Comment Utility
file.Close() is missing at the end of code.
0
 
LVL 13

Assisted Solution

by:drypz
drypz earned 100 total points
Comment Utility
hi! try this...

        private void button1_Click(object sender, EventArgs e)
        {
            string[] strArray = new string[] {"val1","val2","val3"};
            string filePath = Application.StartupPath + @"\Output.txt";

            using (System.IO.StreamWriter sw = new System.IO.StreamWriter(filePath))
            {
                foreach (string value in strArray)
                {
                    sw.WriteLine(value);
                }
            }
        }

You can change the file path if you want.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Assisted Solution

by:vaibhav_dw
vaibhav_dw earned 200 total points
Comment Utility
Hi cotj73

Try with attached code.
You can check if the file is existing and open it FileMode.Append mode.

string[] arr = { "text1", "text2" };
 

FileStream f = new FileStream("d://output.txt", FileMode.);

StreamWriter s = new StreamWriter(f);
 

foreach (string temp in arr)

{

    s.WriteLine(temp);

}
 

s.Close();

f.Close();

    

Open in new window

0
 
LVL 1

Assisted Solution

by:vaibhav_dw
vaibhav_dw earned 200 total points
Comment Utility
Hi

I just missed the mode in above answer.
Dont forget to close both the streams.
string[] arr = { "text1", "text2" };

 

FileStream f = new FileStream("d://output.txt", FileMode.Create);

StreamWriter s = new StreamWriter(f);

 

foreach (string temp in arr)

{

    s.WriteLine(temp);

}

 

s.Close();

f.Close();

Open in new window

0
 
LVL 20

Assisted Solution

by:ddayx10
ddayx10 earned 100 total points
Comment Utility
I think we're all on the same path here,but if that gives you any trouble here is another (lazy) way to go. I dunno how you're trying to layout the text but this should give ya the idea:

        string[] myArray = new string[]{"One,Two","Buckle,My,Shoe"};

        StreamWriter sw = File.CreateText(Server.MapPath("~/files/") + "newFile.txt");
        foreach (string str in myArray)
        {
            string split = ",";
            char[] c = split.ToCharArray();
            string[] myArray2 = str.Split(c);
            foreach (string str2 in myArray2)
            {
                sw.WriteLine(str2);
            }
        }
        sw.Close();
0
 
LVL 20

Expert Comment

by:ddayx10
Comment Utility
I think it goes without saying that for all these solutions you need to get the namespace:

using system.io;
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now