跨2个应用程序访问单个文件 [英] Access single file across 2 application
本文介绍了跨2个应用程序访问单个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个要求,其中我必须跨2个应用程序读取/写入单个xml文件,其中一个是Windows窗体应用程序,另一个是Windows服务。是否有任何最佳方法或设计模式定义来实现这一点,这将确保当两个应用程序都尝试访问此文件时没有死锁。在Windows服务方面,我想到了根据文件的最后更新时间信息使用事件驱动机制。
解决方案
这是一个坏主意有多个程序同时写入同一个文件。
如果你只是阅读那么你可以:
1)首先将整个文件读入内存然后使用File.ReadAllText()进行处理(如果它足够小)
2 )以共享读取模式打开文件: http://msdn.microsoft .com / zh-CN / library / y973b725%28v = vs.110%29.aspx [ ^ ]
要检测文件更改,请使用FileSystemWatcher
: FileSystemWatcher - Pure Chaos(Part 1 of 2) [ ^ ]
你可以试试这个: http://msdn.microsoft.com/en-us/library/ee617386(v = sql.110).aspx [ ^ ]
另一种方式...
http://blog.markedup.com/2014/07/easy-mode-synchronizing-multiple-processes-with-file-locks/ [ ^ ]
I have a requirement wherein i have to read/write a single xml file across 2 application, one of which is a windows form application and others an windows service. Is there any best approach or a design pattern defined to achieve this, which will make sure there is no deadlock when both apps try to access this file. On the windows service side, I thought of making use of event driven mechanism based upon the "last updated time" info of the file.
解决方案
It's a bad idea to have multiple programs write to the same file at the same time.
If you are only reading then you can :
1) Read the entire file to memory first then process (if it is small enough) withFile.ReadAllText()
2) Open the file in shared read mode : http://msdn.microsoft.com/en-us/library/y973b725%28v=vs.110%29.aspx[^]
To detect file changes use theFileSystemWatcher
: FileSystemWatcher - Pure Chaos (Part 1 of 2)[^]
You can try this : http://msdn.microsoft.com/en-us/library/ee617386(v=sql.110).aspx[^]
Another Way...
http://blog.markedup.com/2014/07/easy-mode-synchronizing-multiple-processes-with-file-locks/[^]
这篇关于跨2个应用程序访问单个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文