C# 当可执行文件与数据文件不在同一文件夹中时,追加到文件失败
问题现在解决了。我犯了一个我以前没见过的错误 一般来说,我对编码非常陌生,对C语言也非常陌生,所以我可能遗漏了一些简单的东西。我编写了一个程序,从登录网站中提取数据,并将数据保存到本地硬盘上的文件中。数据是太阳能模块的功率和能量数据,每个模块都有自己的文件。在我的主工作站上,我运行的是WindowsVista,程序运行得很好。当我在运行Server2003的机器上运行该程序时,它不会将新数据附加到文件中,而是覆盖文件中最初的数据 我下载的数据是csv格式的文本,每次7天。我每天运行一次程序来提取新一天的数据并将其附加到本地文件中。每次我运行程序时,本地文件都是新下载数据的副本,没有旧数据。由于网站上的数据每天只更新一次,我一直在通过删除本地文件中最后一天的数据和/或本地文件中第一天的数据进行测试。每当我更改文件并运行程序时,该文件包含下载的数据,而不包含其他内容 我只是尝试了一些新的东西来测试它为什么不起作用,我想我已经找到了错误的根源。当我在本地计算机上运行时,“filePath”变量被设置为“”。在服务器上,现在在我的本地机器上,我已将“文件路径”更改为@“C:\Solar Yard Data\”,并且在这两台机器上,它捕获文件未找到异常,并在同一目录中创建一个新文件,覆盖原始文件。有人知道为什么会这样吗 代码是下载每个数据集并将任何新数据附加到本地文件的部分C# 当可执行文件与数据文件不在同一文件夹中时,追加到文件失败,c#,file-io,C#,File Io,问题现在解决了。我犯了一个我以前没见过的错误 一般来说,我对编码非常陌生,对C语言也非常陌生,所以我可能遗漏了一些简单的东西。我编写了一个程序,从登录网站中提取数据,并将数据保存到本地硬盘上的文件中。数据是太阳能模块的功率和能量数据,每个模块都有自己的文件。在我的主工作站上,我运行的是WindowsVista,程序运行得很好。当我在运行Server2003的机器上运行该程序时,它不会将新数据附加到文件中,而是覆盖文件中最初的数据 我下载的数据是csv格式的文本,每次7天。我每天运行一次程序来提取
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");
我认为有几个建议可能会解决这个问题 首先更改文件路径字符串 字符串文件路径=@“C:\Solar Yard Data\” 创建具有完整路径的字符串
String fullFilePath = filePath + fileName;
然后检查它是否存在,如果不存在,则创建它
if (!File.Exists(fullFilePath ))
File.Create(fullFilePath );
将文件的完整路径放入streamReader中
using (StreamReader sr2 = new StreamReader(fullFilePath))
我认为有几个建议可能会解决这个问题 首先更改文件路径字符串 字符串文件路径=@“C:\Solar Yard Data\” 创建具有完整路径的字符串
String fullFilePath = filePath + fileName;
然后检查它是否存在,如果不存在,则创建它
if (!File.Exists(fullFilePath ))
File.Create(fullFilePath );
将文件的完整路径放入streamReader中
using (StreamReader sr2 = new StreamReader(fullFilePath))
问:与其将“sr2”创建为“StreamReader()”(并调用AppendText),不如打开“StreamWriter sw=newstreamwriter(filename)”,然后将其追加到其中?只需打开两个文件:一个从HTTP读取,另一个附加到.csv。这就是你所需要的——没有别的。我只需要附加新的数据,下载的数据将包含很多已经保存的数据。我使用“StreamReader sr2”查找最后一个记录的条目,然后搜索该条目的新数据,并将之后的所有内容附加到本地文件中。问:与其将“sr2”创建为“StreamReader()”(并调用AppendText),不如打开“StreamWriter sw=new StreamWriter(filename)”,然后附加到它?只需打开两个文件:一个从HTTP读取,另一个附加到.csv。这就是你所需要的——没有别的。我只需要附加新的数据,下载的数据将包含很多已经保存的数据。我使用“StreamReader sr2”来查找最后记录的条目,然后搜索该条目的新数据,并将之后的所有内容附加到本地文件中。是的,谢谢。这就是我遇到的问题。我一直在更改路径,但忘记了StreamReader对象,它抛出了FileNotFound异常,导致新文件覆盖旧文件。是的,谢谢。这就是我遇到的问题。我不断更改路径,但忘记了StreamReader对象,它抛出FileNotFound异常,导致新文件覆盖旧文件。