动态文件读取C# [英] Dynamic file reading C#

查看:104
本文介绍了动态文件读取C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于通过GUI显示传感器值的Windows窗体应用程序。我的GUI输入是一个文本文件,由传感器询问器不断更新。我读取文本文件,将其保存到列表中并对其进行计算和操作。

目前我的应用程序仅适用于静态txt文件,我希望它能够直接从不断更新的txt文件。

可以这样做吗?

我已经尝试过使用FilsSstemWatcher类,但没什么用。



我试过从代码项目文章中获取想法 在实际场景中使用FileSystemWatcher [ ^ ]

I have a windows form application for displaying sensor values through a GUI. The input for my GUI is a text file that is being continuously updated by a Sensor interrogator. I read the text file, save it onto a list and do my calculations and manipulations on it.
At present my application works only for a static txt file, I want it to be capable of directly reading from the continuously updated txt file.
Can this be done?
I already tried using the FilsSstemWatcher class but to no much use.

I tried taking ideas from a codeproject article Use FileSystemWatcher in a Practical Scenario[^]

推荐答案

取决于。如果编写文件的软件被写入以随时对文件进行独占锁定,则会出现问题:要么在更新时无法访问该文件,要么传感器赢了在你阅读的时候能够更新它并且数据可能会丢失。



写文件通常涉及独占锁(所以当数据不会被破坏时它是由不同的应用程序写的一半)但确切地说这是如何完成的。有些应用程序在启动时会进行独占锁定,并在关闭时释放它们(Outlook会烦恼地使用它的数据库)。其他人拿锁,写数据并释放锁(Word这样做)。如果您的软件像后者那样工作,那么您应该能够读取文件,但如果传感器软件无法很好地解决锁定故障,这可能会导致问题 - 如果它没有重试,它可能会丢失数据。基本上,你将不得不尝试它,看看会发生什么!



缺少单词:file - OriginalGriff [/ edit]
Depends. If the software writing the file is written to take an exclusive lock on the file at any time, then something is going to break: either you won''t be able to access the file while it is being updated, or the sensor won''t be able to update it while you are reading and data could be lost.

Writing files often involves exclusive locks (so the data doesn''t get corrupted when it is half written by a different application) but exactly how this is done varies. Some applications take an exclusive lock when they start up, and release it when they close (Outlook does this with it''s databases annoyingly). Others take the lock, write the data and release the lock (Word does this). If your software works like the latter, then you should be able to read the file, but this may cause problems if the sensor software does not cope well with lock failure - if it doesn''t retry, it may well lose data. Basically, you will have to try it and see what happens!

[edit]Missing word: "file" - OriginalGriff[/edit]


这篇关于动态文件读取C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆