using System;

using WebApplication1.mdws;
using System.IO;
using System.Text;

namespace WebApplication1
{
public class MdwsDao
{
QuerySvc _svc;

public MdwsDao()
{
_svc = new QuerySvc();
_svc.CookieContainer = new System.Net.CookieContainer();

}

public void connectAsJohn()
{
_svc.visitSite("foo", "523", "523", "SMITH,JOHN", "3050134", "999999999", "");

}


public string[] ddr(string vistaFile, string vistaFields, string flags, string from, string to, string xref)
{
TextArray ta = _svc.ddrLister("230", "", ".01;.02;.03", "IPB", "10", "", "", "", "", "");


foreach (string s in ta.text)
{
Console.WriteLine(s);
}
Console.ReadLine();

return ta.text;

}


}
}

Help! I started two learn C# two days ago as I'm in a crunch to finish a project and of course IT where I work doesn't know a single programming language. Sooo...here's what I know...

The System.IO namespace will have the classes dealing with reading and writing files. http://msdn.microsoft.com/en-us/library/system.io.aspx

Since `ta.text` is a string array, I'm assuming File.WriteAllLines is the most straightforward way to write a string[] to a file. http://msdn.microsoft.com/en-us/library/92e05ft3.aspx

But I can't get it to work no matter what I try...! Can someone show me?

Recommended Answers

All 5 Replies

I wouldn't use WriteAllLines all though it sounds as if it would be useful. I would simply create a foreach loop that goes through the lines of text in 'ta' and writes it to the fie.

foreach (string line in ta.Text)
{
//Put writing code here
}

But that's just me; I don't generally deal with System.IO so I can't be too much help, but with a little reading on Google about how to save your files using System.IO you can implement it into a foreach, or find exact information on what you're looking for. I hope this gives you a slight push in the right direction.

Good Luck,
Jamie

***EDIT***
Okay, I revised your post (as I usually do after replying to ensure my reply is accurate) and I thought you were using a text box control. But seeing as you're using TextArray to do this I have no clue because I've never heard of the namespace. Probably have used it without knowing, but never have actually caught the name in full view like that. Sorry I can't be more assistance. I also don't see anything trying to save the file in there, but I may be just glimpsing too quickly.

Jamie

***EDIT***
Look into StreamReader/StreamWriter. It might help out:

Read

// Specify file, instructions, and privelegdes
file = new FileStream("test.txt", FileMode.OpenOrCreate, FileAccess.Read);

// Create a new stream to read from a file
StreamReader sr = new StreamReader(file);

// Read contents of file into a string
string s = sr.ReadToEnd();

// Close StreamReader
sr.Close();

// Close file
file.Close();

Write

// Specify file, instructions, and privelegdes
FileStream file = new FileStream("test.txt", FileMode.OpenOrCreate, FileAccess.Write);
            
// Create a new stream to write to the file
StreamWriter sw = new StreamWriter(file);

// Write a string to the file
sw.Write("Hello file system world!");

// Close StreamWriter
sw.Close();

// Close file
file.Close();

For more examples check MSDN for Stream Reader and Stream Writer. Also can be implemented with Open and Save File Dialogs to ease reading and writing.

Jamie

I didn't even include my attempt to use the System.IO stuff, but essentially I tried every combination of File.AppendAllText(path, appendText); within the foreach...and nothin'.

So your suggestion was exactly what I've been trying.

I've updated my post to try and help you further. You should look into StreamReader and StreamWriter. They might be useful to you; they are both within the System.IO namespace.

LINQ makes dealing with problems like this simple.

public Form1()
        {
            InitializeComponent();
            string[] mydata = new string[] {"2", "3", "4"};
            WriteStringsToFile("text.txt",mydata);
        }

        public void WriteStringsToFile(string FileName, ICollection<string> myStringCollection, bool append = true)
        {
            using (StreamWriter sr = new StreamWriter(FileName,append))
            {
                myStringCollection.ToList<string>().ForEach(x => sr.WriteLine(x));
                sr.Close();
            }
        }

Let me know if it needs explaining.

Assuming you're just trying to write to a file...

using System;
using System.IO;

namespace DW_408731_CS_CON
{
   class Program
   {
      static void Main(string[] args)
      {
         string[] arr_strData = { "230", "", ".01;.02;.03", "IPB", "10", "", "", "", "", "" };
         string strTempFile = Path.GetTempFileName();

         Console.WriteLine("Writing all lines to: " + strTempFile);
         File.WriteAllLines(strTempFile, arr_strData);


         Console.WriteLine("Press enter to delete the temp file");
         Console.ReadLine();

         File.Delete(strTempFile);
      }
   }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.