[英]Read and write to the same csv file
I have a CSV file (Eg Directories.csv) which contains a huge list of directories.我有一个包含大量目录的 CSV 文件(例如 Directories.csv)。 I am looping through the directories from the CSV using streamreader and performing some task.我正在使用流阅读器从 CSV 中遍历目录并执行一些任务。 I am updating the completed directory list to a dictionary and stuck at this step now.我正在将完成的目录列表更新为字典并现在停留在这一步。
Ask: I want to capture the data through the loop on which directories are complete in the same CSV just in case the application crashes or server reboots, so that I don't have to re-iterate through the loop again which got completed.问:我想通过在同一 CSV中完成目录的循环捕获数据,以防应用程序崩溃或服务器重新启动,这样我就不必再次遍历已完成的循环。 (Or) Delete the completed directories row from the CSV (或)从 CSV 中删除已完成的目录行
I tried to check online for suggestions and asking to create temp file and move the copy of it.我试图在线检查建议并要求创建临时文件并移动它的副本。 Can this be possible in case the server reboots or application crashes?如果服务器重新启动或应用程序崩溃,这可能吗? Please suggest how can I take this forward.请建议我如何向前推进。
My code:我的代码:
Dictionary<string, string> directoryDictionary = new Dictionary<string, string>();
using (FileStream fileStreamDirectory = File.Open(outputdir + "\\Directories.csv", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (BufferedStream bufferStreamDirectory = new BufferedStream(fileStreamDirectory))
using (StreamReader streamReaderDirectory = new StreamReader(bufferStreamDirectory))
{
while ((Directoryline = streamReaderDirectory.ReadLine()) != null)
{
#Doing the task here
directoryDictionary.Add(Directoryline, "Completed");
}
}
You can't really insert data into middle of a text file (unless it is fixed width format which in not the case of CSV).您不能真正将数据插入文本文件的中间(除非它是固定宽度格式,而 CSV 则不是)。
Two options:两种选择:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.