在同一过程中的同一文件上创建2 FileStream [英] Create 2 FileStream on the same file in the same process

查看:104
本文介绍了在同一过程中的同一文件上创建2 FileStream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个临时文件,该文件将被自动删除.

I'm trying to create a temporary file that will be automatically deleted.

stream = new FileStream(
           tmpFilePath, 
           FileMode.OpenOrCreate, 
           FileAccess.ReadWrite, 
           FileShare.ReadWrite, 
           4096, 
           FileOptions.DeleteOnClose|FileOptions.RandomAccess
           );

此文件将由第三方API使用,该API也将创建FileStream:

This file will be used by a 3rd party API which will also create a FileStream:

stream = new FileStream(
          tmpFilePath, 
          FileMode.Open, 
          FileAccess.Read, 
          FileShare.Read);

我想我已经尝试了所有可能的标志组合,但是我总是得到一个该进程无法访问文件'XXX',因为它正在被另一个进程使用..."

I think I've tried all possible combination of flags but I always get a "The process cannot access the file 'XXX' because it is being used by another process..."

我做错什么了吗?有办法吗?

Am I doing something wrong? Is there a way around?

推荐答案

根据文档,是的.

http://msdn.microsoft.com/zh-我们/library/system.io.fileshare.aspx

节选:

读取:允许随后打开文件进行读取.如果未指定此标志,则任何打开文件以供读取的请求(通过此过程或其他过程)将失败,直到关闭该文件.但是,即使指定了此标志,仍可能需要其他权限才能访问文件.

Read: Allows subsequent opening of the file for reading. If this flag is not specified, any request to open the file for reading (by this process or another process) will fail until the file is closed. However, even if this flag is specified, additional permissions might still be needed to access the file.

这篇关于在同一过程中的同一文件上创建2 FileStream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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