简体   繁体   中英

Creating and saving a text file on a server every day through a C# console application

I have developed a C# console application which I scheduled it on a windows server which runs every morning and creates a text file in a folder named FTP in C drive of the server with some data retrieved from SQL server and this file will be accessed by some outside users using FTP . I have used the following code

 FileStream fs = File.Create("test.txt");

            int i = 0;
            StreamWriter sw = null;

            sw = new StreamWriter("C:\FTP\test.txt", false);
            for (i = 0; i < dtlist.Columns.Count - 1; i++)
            {
                sw.Write(dtlist.Columns[i].ColumnName + "\t");
            }
            sw.Write(dtlist.Columns[i].ColumnName);
            sw.WriteLine();


            foreach (DataRow row in dtlist.Rows)
            {
                object[] array = row.ItemArray;

                for (i = 0; i < array.Length - 1; i++)
                {
                    sw.Write(array[i].ToString() + "\t");
                }
                sw.Write(array[i].ToString());
                sw.WriteLine();
            }

            sw.Close();

As we can see that I have directly given the file path on the server as I scheduled this application on the server itself and I wonder if this the right approach or if I should better user the server address 10.0.0.10 as the destination and create a file.

May I know a better way if there is any?

While this is probably more a code review question rather than stack overflow...

Personally, Id not hard code the resulting filename, but have it as a parameter, so if you have to move it, thats fine no re-coding required. You could consider FTP'ing the file to the server, however, then you get into the realms of securely storing usernames and passwords potentially, but it is a consideration.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM