问题现在解决了。我误以为我以前没有见过。
我对编码很新,对C#来说很新,所以我可能会遗漏一些简单的东西。我写了一个程序从登录网站提取数据并将数据保存到本地硬盘上的文件中。数据是太阳能模块的功率和能量数据,每个模块都有自己的文件。在我的主工作站上,我正在运行Windows Vista,程序运行正常。当我在运行Server 2003的计算机上运行该程序时,它不会将新数据附加到文件中,而只是覆盖文件中最初的数据。
我下载的数据是csv格式文本,每次7天。我每天运行一次程序来拉取新的一天的数据并将其附加到本地文件。每次运行程序时,本地文件都是新下载数据的副本,没有旧数据。由于网站上的数据每天只更新一次,我一直在测试,删除本地文件中的最后一天的数据和/或本地文件中的第一天的数据。每当我更改文件并运行程序时,该文件都包含下载的数据,而不包含任何其他内容。
我刚试了一些新的东西来测试它为什么不起作用,并认为我找到了错误的来源。当我在本地计算机上运行时,“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");
答案 0 :(得分: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))