不同 Windows PC 上的多个进程应如何同时使用存储在共享目录中的文件?
问题:
我在同一 LAN 中的不同 PC(操作系统:Windows XP、Windows 7)上运行同一 C# 应用程序的多个实例。我必须在他们之间共享一些配置数据。每个进程都必须具有对数据的读写访问权限。我的雇主坚持将这些共享数据存储在一个文件中,该文件位于其中一台电脑的共享目录中。
可能的解决方案:
- 独占文件打开:数据存储在 TXT 文件中(也可以选择与二进制文件进行序列化)。每个进程在尝试打开文件时都使用
File.Open
和FileShare.None
。获取 IOException 意味着该文件已在使用中,因此该过程必须 等待并稍后重试。 - SQL Server CE嵌入式数据库< /a>:数据存储在SDF文件中。该引擎最多可以处理 256 个同时连接(v3.5 SP2),这已经足够了。
- SQLite 嵌入式数据库:数据存储在 SQLite 数据库文件中。 文档说 SQLite 可以工作,但在网络共享上使用时可能不可靠。
- 其他?
执行此操作的首选方法是什么?
Problem:
I have multiple instances of the same C# application running on different PCs (OS: Windows XP, Windows 7) in the same LAN. I have to share some configuration data among them. Each process must have read-write access to the data. My employer insists on storing these shared data in a file, which is in a shared directory on one of these PCs.
Possible solutions:
- Exclusive file opening: The data is stored in a TXT file (serialization to and from a binary file is also an option). Each process uses
File.Open
withFileShare.None
when trying to open the file. Getting anIOException
means that the file is already in use, so the process has to wait and try again later. - SQL Server CE embedded DB: The data is stored in an SDF file. The engine can handle at most 256 simultaneous connections (v3.5 SP2), which is more than enough.
- SQLite embedded DB: The data is stored in an SQLite DB file. The documentation says SQLite works, but may be unreliable when used on a network share.
- Other?
What is the preferred way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不知道这是否是最好的方法,但我很久以前就用 C 语言做过这个,它对我来说效果很好。
每个进程都会读取并创建文件的个人副本,然后对其进行处理。
在固定时刻(进程终止或通过某些 UI 或任何您喜欢的方式触发时),每个进程都会将其文件副本发送到负责在共享目录中重建原始文件并向其他进程发出信号的主进程。需要重新加载。
每个进程都会重新加载文件(包含来自所有其他进程的信息)。
当然,这个解决方案要求文件写入过程了解如何重建文件以及如何解决冲突(但这取决于数据格式)
Don't know if is the best way, but I've done this in C ages ago, it was working well for me.
Each process will read and create a personal copy of the file and then work on that.
At a fixed moment (upon process termination or triggered via some UI or whatever you feel like) each process will send its copy of the file to a master process in charge of rebuilding the original file in the shared directory and signaling the other process that they need to reload.
Each process reloads the file (containing infos coming from all the other processes).
Of course this solution requires that the file writing process has knowledge on how to rebuild the file and how to resolve conflicts (but this depends on data format)
您并没有真正描述您正在使用的数据的类型,所以我想说答案会有所不同。
如果您正在使用的数据通常被认为是面向记录/字段的(并且在极少数情况下,即使不是面向记录/字段),那么使用适当的 DBMS 是最好的。在这种情况下,我会推荐 MSSQL CE,因为它的运行时将为您缓解多用户问题。
SQLite 通常被认为是单个用户/应用程序数据库(至少当我在 C 中使用它时),尽管在过去 5 年里情况可能发生了变化。如果您使用的是 .NET 4,那么我发现几乎没有可用的免费适配器,除非您对混合框架应用程序感到满意。
如果您处于数据设计相当扁平的情况(如日志文件),我只会手动监视文件锁定,但如果它是类似日志的数据,我可能会研究一些开源日志库如何做吧。您基本上说您可以控制数据结构,因此我建议重新设计数据以使其更加标准化/刚性,以避免使用此解决方案。
You don't really describe the type of data you're working with so I'd say the answer varies.
Using a proper DBMS for this would be best if the data you are working with could generally be considered record/field oriented (and under rare circumstance even if it isn't). In this case I would recommend MSSQL CE since its runtime will mitigate multi-user issues for you.
SQLite was generally considered a single user/application database (at least back when I used it in C) though things could have changed in the last 5 years. If you're using .NET 4 then there are few free adapters available for use from what I've found unless you're comfortable with a mixed framework application.
I would only monitor the file locking manually if you're in a situation where the data is pretty flat by design (like a log file), though if it was log like data I would probably look into how some of the open source log libraries do it. You basically said you have control over the data structure so I'd suggest redesigning the data to be more normalized/rigid to avoid using this solution.
创建一个 Web 服务并让您的程序从那里提取配置。您可以从 Web 服务内部控制文件锁定,而不必在程序级别处理该问题。这还为您提供了一个抽象概念,即如果您决定更改设置的存储方式(例如将它们从文件移动到数据库),则无需对程序进行任何更改即可执行此操作。
Create a web service and make your programs pull the configuration from there. You can control file locking from inside the web service and not have to deal with that at the program level. This also affords you the abstraction that if you decide to change how the settings are stored (e.g. move them from a file to a database) you can do this without having to make any changes to your program.