gpt4 book ai didi

c# - 当可执行文件与数据文件不在同一文件夹中时附加到文件失败

转载 作者:太空宇宙 更新时间:2023-11-03 16:29:34 25 4
gpt4 key购买 nike

问题已经解决了。我以前没见过的错误。

我对一般编码还很陌生,对 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");

最佳答案

一些我认为可能会解决问题的建议首先更改您的文件路径字符串

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

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

String fullFilePath = filePath + fileName;

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

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

将文件的完整路径放入 streamReader

using (StreamReader sr2 = new StreamReader(fullFilePath))

关于c# - 当可执行文件与数据文件不在同一文件夹中时附加到文件失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11254437/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com