简体   繁体   English

每天通过C#控制台应用程序在服务器上创建和保存文本文件

[英]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 . 我已经开发了一个C# console application ,将它安排在每天早晨运行的Windows服务器上,并在服务器的C drive中的名为FTP的文件夹中创建一个文本文件,其中包含从SQL server检索到的一些数据,并且某些人可以访问该文件。外部用户使用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. 如我们所见,我在服务器上安排此应用程序时直接提供了服务器上的文件路径,我想知道这是否正确,还是我应该更好地将服务器地址10.0.0.10作为目标并创建一个文件。

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. 您可以考虑通过FTP将文件传输到服务器,然后进入安全地存储用户名和密码的领域,但这是一个考虑因素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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