How to Change the name of a Text File in a loop using visual c# and streamwriter

475 Views Asked by At

I am trying to write a loop that reads through a list, and saves parts of that list to different text files based on the hour the data was taken. I want to name the text file based on the hour that the data was taken.

Here is the relevant code:

private StreamWriter filename;
string[] hour = {"00","01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23}

private void btn_hourfiles_click(object sender, EventArgs e)
{
    int sizeoflist = Data_List.Count(); //This is the list containing the data
    for (int i = 0; i<sizeoflist,i++)
    {
       string[] listsplit = Data_List[i].Split(":");
       for (int j = 0; j<24; j++)
       {
           if (listsplit[2] == hour[j])
           {
            string[] datesplit = txt_DATE.Text.Split('-');//splits input date
            filename = datesplit[0] + '_' + dateplit[1] + '_' + datesplit[2] + '_' + hour[j] + '.dat'
            filename = new StreamWriter(new FileStream(Data_List[i], FileMode.Append, FileAccess.Write));
            }
       }
    }
 }

The problem: I can't change the StreamWriter filename. I get an error that says: Cannot implicitly convert type string to System.IO.StreamWriter

Question: How can I change the variable filename?

2

There are 2 best solutions below

0
ehh On BEST ANSWER

The code you post is not getting compiled. Try next time to post the input so it will be simpler to help and answer to your question. Anyway, I tried to help you according to what you wrote above, hope it will help you

class Program {

    static void Main(string[] args)
    {
        MyTest test = new MyTest();

        test.Foo();
    }
}

class MyTest
{
    private StreamWriter streamWriter;
    string[] hour = { "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23" };

    public void Foo()
    {
        string txt_DATE = "25-02-2015";
        string[] Data_List = { "12:05:10", "02:15:00", "14:15:22" };

        int sizeoflist = 3; // Data_List.Count(); //This is the list containing the data
        for (int i = 0; i < sizeoflist; i++)
        {
            string[] listsplit = Data_List[i].Split(':');
            for (int j = 0; j < 24; j++)
            {
                if (listsplit[2] == hour[j])
                {
                    string[] datesplit = txt_DATE.Split('-');//splits input date
                    var filename = datesplit[0] + '_' + datesplit[1] + '_' + datesplit[2] + '_' + hour[j] + ".dat";
                    streamWriter = new StreamWriter(new FileStream("C:\\" + filename, FileMode.Append, FileAccess.Write));
                }
            }
        }
    }
}
0
bright On

The problem is that StreamWriter is declared thusly:

private StreamWriter filename;

but then assigned a string:

filename = datesplit[0] + '_' + dateplit[1] + '_' + datesplit[2] + '_' + hour[j] + '.dat'

Suggestion: use separate variables for filename and for the StreamWriter.