C# 程序从同一文件读取的多个实例

发布于 2024-12-11 04:57:07 字数 143 浏览 0 评论 0原文

我编写了一个程序,它从文件中读取,根据给定的参数处理它并写入一些输出。我很好奇,当用户创建我的程序的更多实例(通过打开可执行文件)并在同一个文件上运行它们时,是否会出现任何奇怪的问题。

我使用 while 循环和 file.ReadLine() 读取文件。

I wrote a program that reads from file, processes it according to given parameters and writes some output. I was curious, if there can occur any strange problems when user creates more instances of my program (by opening executable file) and runs them over the same file.

I read from file with while loop and file.ReadLine().

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

请别遗忘我 2024-12-18 04:57:07

我很好奇,当用户创建我的程序的更多实例(通过打开可执行文件)并在同一个文件上运行它们时,是否会出现任何奇怪的问题。

这种措辞方式让我认为您担心用户可能会意外地针对同一文件运行程序的多个实例。如果这是您关心的问题,那么您可以通过以独占模式打开文件来防止出现问题。只要文件保持打开状态,操作系统本身就会确保只有程序的单个实例才能访问该文件。

如果您使用 FileStream 对象访问文件,那么我相信独占模式是默认的,因此您根本不必担心这一点。只需确保您的流在需要一致性的所有读取和写入过程中保持打开状态即可。

如果程序的另一个实例尝试打开该文件,那么它将抛出 IOException 并且不允许访问。您可以让程序崩溃,也可以通知用户该文件已被使用,并让他们选择另一个文件,或者其他什么。

更新

如果您想在程序的多个实例中读取同一个文件,而没有实例写入它,那么这也很容易实现。只要以共享只读模式打开文件,让很多程序同时读取该文件是完全没有问题的。

using (Stream stream = new FileStream(filePath, FileMode.Open, 
                                      FileAccess.Read, FileShare.Read))

I was curious, if there can occur any strange problems when user creates more instances of my program (by opening executable file) and runs them over the same file.

The way this is worded leads me to think that you are concerned that the user might accidently run multiple instances of the program against the same file. If that is your concern, then you can prevent problems from this by opening the file in an exclusive mode. The operating system itself will ensure that only the single instance of the program has access to the file for as long as you hold the file open.

If you access the file with a FileStream object, then I believe that exclusive mode is the default, so you don't have to worry about this at all. Just make sure your stream stays open through all the reads and writes for which consistency is required.

If another instance of the program attempts to open the file, then it will throw a IOException and not allow the access. You can either let the program crash, or you can notify the user that the file is already being used and give them the option to choose another file, or whatever.

UPDATE

If you want to read from the same file in many instances of the program, with no instance writing to it, then that is also easily doable. Just open the file in shared read-only mode, and there is no problem at all letting lots of programs read the file simultaneously.

using (Stream stream = new FileStream(filePath, FileMode.Open, 
                                      FileAccess.Read, FileShare.Read))
楠木可依 2024-12-18 04:57:07

如果他们都在读取,那么只有锁定文件才会出现问题。使用 FileAccess.ReadFileShare.Read 将允许它们同时打开它。

If they're all reading then there will only be a problem if you lock the file. Using FileAccess.Read and FileShare.Read will allow them to all open it at the same time.

我一向站在原地 2024-12-18 04:57:07

如果您只是读取文件,那么应该不会有任何问题。

但是,您可以使用 Mutex< /a> 如果您希望只有一个程序能够同时处理该文件。

例如:

Boolean isMyFileFree;
using (Mutex mutex = new Mutex(true, "Mutex4MyFile", out isMyFileFree)) {
    // Process your file
}

There shouldn't be any problem if you are only reading from the file.

However, you could use a Mutex if you wished that only one program were able to process the file at the same time.

For instance:

Boolean isMyFileFree;
using (Mutex mutex = new Mutex(true, "Mutex4MyFile", out isMyFileFree)) {
    // Process your file
}
鲸落 2024-12-18 04:57:07

由于这两个实例都可能将数据写入文件,因此您处于非常危险的境地:在读取和写入时都可能会损坏数据。

您应该做的是获得适当的权限/访问权限;例如,如果您使用 File.Open< /a> 打开文件,然后指定 FileAccessFileShare 表示适用于当前的执行模式。

现在,由您来处理应用程序中的可访问性 - 当其中一个实例被“锁定”文件时,有一个可以回退的过程,以免中断(这与损坏数据一样糟糕)毫无疑问。)

如果您希望应用程序保持完整性,您的处理很可能只是暂停一段时间并超时,然后重试;或者您可以删除冲突实例将要写入的更改。这取决于你的情况,但你有责任并且必须处理它。

Since both instances potentially write data to the file, you are very much in danger: both in reading and writing there may be corrupt data.

What you ought to do is acquire appropriate permissions / access; for instance, if you're using File.Open to open the file then specify the FileAccess and FileShare that is applicable to the current mode of execution.

Now, it is up to you to handle accessibility in the application - have a course to fallback on when one of the instances is "locked out" of the file, so as not to break (which would be just as bad as corrupting the data, no doubt.)

Your handling might well just be to go on hold for a certain timespan with a timeout, and retry, if you want your application to maintain integrity; or you could just drop changes that the conflicting instance would be going to write. This depends on your situation, but you are responsible and do have to handle it.

(り薆情海 2024-12-18 04:57:07

如果您使用 File.Open 并传递除了 FileShare.None 之外的其他内容到第四个 share 参数,那么其他线程或进程就可以访问该文件。

如果其他人只有读取访问权限,那么这根本没有问题,除非您想删除该文件。如果其他人具有更高级别的访问权限,那么您的代码应该处理其他可能性,例如文件被更改或删除。

请注意,指定 FileShare.ReadWrite 可以扩展对所有拥有文件权限的进程的访问权限,而不仅仅是您知道的进程。

If you using File.Open and passing somthing other than FileShare.None to the 4th share paramter then it could be possible for other threads or processes to access the file.

If the others only have Read access this will be no problem at all, except for when you want to delete the file. If the others have a higher level of access then your code should deal with the alternative possibilities, like the file being altered or deleted.

Note that specifying FileShare.ReadWrite extends access to all process that have rights to file, not just those you know of.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文