2

问题现在解决了。我以前从未见过的错误。

一般来说,我对编码很陌生,对 C# 也很陌生,所以我可能遗漏了一些简单的东西。我编写了一个程序来从登录网站提取数据并将该数据保存到本地硬盘上的文件中。数据是太阳能组件的功率和能源数据,每个组件都有自己的文件。在我的主工作站上,我运行的是 Windows Vista,程序运行良好。当我在运行 Server 2003 的机器上运行程序时,它不会将新数据附加到文件中,而是会覆盖文件中最初的数据。

我正在下载的数据是一次 7 天的 csv 格式文本。我每天运行一次程序以提取新一天的数据并将其附加到本地文件中。每次我运行程序时,本地文件都是新下载数据的副本,没有旧数据。由于网站上的数据每天只更新一次,我一直在通过删除本地文件中最后一天的数据和/或本地文件中第一天的数据进行测试。每当我更改文件并运行程序时,该文件都包含下载的数据,仅此而已。

我只是尝试了一些新的东西来测试它为什么不起作用,并认为我找到了错误的根源。当我在本地机器上运行时,“filePath”变量设置为“”。在服务器上,现在在我的本地机器上,我已将“filePath”更改为@“C:\Solar Yard Data\”,并且在两台机器上它都会捕获文件未找到异常并在同一目录中创建一个新文件,该文件会覆盖原来的。任何人都知道为什么会发生这种情况?

代码是下载每个数据集并将任何新数据附加到本地文件的部分。

int i = 0;
string filePath = "C:/Solar Yard Data/";

string[] filenamesPower = new string[]
{
    "inverter121201321745_power",
    "inverter121201325108_power",
    "inverter121201326383_power",
    "inverter121201326218_power",
    "inverter121201323111_power",
    "inverter121201324916_power",
    "inverter121201326328_power",
    "inverter121201326031_power",
    "inverter121201325003_power",
    "inverter121201326714_power",
    "inverter121201326351_power",
    "inverter121201323205_power",
    "inverter121201325349_power",
    "inverter121201324856_power",
    "inverter121201325047_power",
    "inverter121201324954_power",
};

// download and save every module's power data
foreach (string url in modulesPower)
{

    // create web request and download data
    HttpWebRequest req_csv = (HttpWebRequest)HttpWebRequest.Create(String.Format(url, auth_token));
    req_csv.CookieContainer = cookie_container;
    HttpWebResponse res_csv = (HttpWebResponse)req_csv.GetResponse();

    // save the data to files
    using (StreamReader sr = new StreamReader(res_csv.GetResponseStream()))
    {
        string response = sr.ReadToEnd();
        string fileName = filenamesPower[i] + ".csv";

        // save the new data to file
        try
        {
            int startIndex = 0;          // start index for substring to append to file
            int searchResultIndex = 0;   // index returned when searching downloaded data for last entry of data on file
            string lastEntry;            // will hold the last entry in the current data
            //open existing file and find last entry
            using (StreamReader sr2 = new StreamReader(fileName))
            {
                //get last line of existing data
                string fileContents = sr2.ReadToEnd();
                string nl = System.Environment.NewLine;  // newline string
                int nllen = nl.Length;                   // length of a newline
                if (fileContents.LastIndexOf(nl) == fileContents.Length - nllen)
                {
                    lastEntry = fileContents.Substring(0, fileContents.Length - nllen).Substring(fileContents.Substring(0, fileContents.Length - nllen).LastIndexOf(nl) + nllen);
                }
                else
                {
                    lastEntry = fileContents.Substring(fileContents.LastIndexOf(nl) + 2);
                }

                // search the new data for the last existing line
                searchResultIndex = response.LastIndexOf(lastEntry);
            }

            // if the downloaded data contains the last record on file, append the new data
            if (searchResultIndex != -1)
            {
                startIndex = searchResultIndex + lastEntry.Length;
                File.AppendAllText(filePath + fileName, response.Substring(startIndex+1));
            }
            // else append all the data
            else
            {
                Console.WriteLine("The last entry of the existing data was not found\nin the downloaded data. Appending all data.");
                File.AppendAllText(filePath + fileName, response.Substring(109));  // the 109 index removes the file header from the new data
            }
        }
        // if there is no file for this module, create the first one
        catch (FileNotFoundException e)
        {
            // write data to file
            Console.WriteLine("File does not exist, creating new data file.");
            File.WriteAllText(filePath + fileName, response);
            //Debug.WriteLine(response);
        }

    }

    Console.WriteLine("Power file " + (i + 1) + " finished.");
    //Debug.WriteLine("File " + (i + 1) + " finished.");
    i++;
}

Console.WriteLine("\nPower data finished!\n");
4

1 回答 1

0

我认为可能会解决问题的几个建议首先更改您的 filePath 字符串

string filePath = @"C:\Solar Yard Data\";

创建一个完整路径的字符串

String fullFilePath = filePath + fileName;

然后检查它是否存在,如果不存在则创建它

if (!File.Exists(fullFilePath ))
     File.Create(fullFilePath );

将文件的完整路径放入您的 streamReader

using (StreamReader sr2 = new StreamReader(fullFilePath))
于 2012-06-29T18:34:56.200 回答