简体   繁体   English

以Notepad.exe的方式打开文件的标志是什么?

[英]Which flags to open a file the way Notepad.exe does?

I'm writing a C# app to read a file that another application holds open. 我正在编写一个C#应用程序来读取另一个应用程序保持打开的文件。 As some of you may guess, all I get is IOExceptions because "the file is being used by another process". 正如你们中的一些人可能猜到的,我得到的只是IOExceptions,因为“该文件正被另一个进程使用”。 I've tried tweaking File.Open() a little; 我试过调整一下File.Open(); this is my current try: 这是我目前的尝试:

FileStream fsIn = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);

(I know that the FileShare flag is more meaningful for other processes that will access the file thereafter, but I have tried with it anyway.) (我知道,文件共享标志是随后访问该文件的其他过程更有意义,但我已经用也无妨尝试。)

What puzzles me is that Notepad.exe will open the file just fine. 令我困惑的是Notepad.exe会打开文件就好了。 Before I dig into Filemon for more clues, does any of you know how to open the file the way Notepad does? 在我深入了解Filemon以获取更多线索之前,您是否知道如何以记事本的方式打开文件? Thanks. 谢谢。

You almost have it. 你几乎拥有它。 You actually want FileShare.ReadWrite : 你真的想要FileShare.ReadWrite

FileStream fsIn = File.Open(fileName,
                            FileMode.Open,
                            FileAccess.Read,
                            FileShare.ReadWrite);

FileShare.Read disallows other processes from writing to that file. FileShare.Read禁止其他进程写入该文件。 FileShare.ReadWrite allows this. FileShare.ReadWrite允许这样做。

Write Allows subsequent opening of the file for writing. 允许随后打开文件进行写入。 If this flag is not specified, any request to open the file for writing (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. 但是,即使指定了此标志,仍可能需要其他权限才能访问该文件。

ReadWrite Allows subsequent opening of the file for reading or writing. ReadWrite允许随后打开文件进行读写。 If this flag is not specified, any request to open the file for reading or writing (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. 但是,即使指定了此标志,仍可能需要其他权限才能访问该文件。

It's backwards to me as well. 这也是我的倒退。

Well, if you are dealing with small files, just use 好吧,如果你正在处理小文件,只需使用

string myFileContents = System.IO.File.ReadAllText(@"c:\daniellesteele.txt");

for text or 用于文本或

byte[] myfileContents = System.IO.File.ReadAllBytes(@"c:\aleagueoftheirmoan.mpg");

for binary files. 用于二进制文件。

Are you sure Notepad can open the file after the other process has opened it? 你确定Notepad可以在其他进程打开打开文件吗? It looks like it's up to who initially owns the file if sharing is granted. 如果共享被授予,它看起来取决于谁最初拥有该文件。 See the following example from MSDN : 请参阅MSDN中的以下示例:

The following FileStream constructor opens an existing file and grants read-only access to other users (FileShare.Read).

[C#] 
FileStream s2 = new FileStream(name, FileMode.Open, FileAccess.Read, FileShare.Read);

Of course I'm not an OS expert, so this may be completely wrong. 当然我不是操作系统专家,所以这可能是完全错误的。

With this 有了这个

FileStream fsIn = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

you allow other programs modify the file, while its open in your program. 你允许其他程序修改文件,同时在你的程序中打开它。 So, if you open the file, the IOException "the file is being used by another process" won't executed. 因此,如果您打开该文件,则不会执行IOException“该文件正由另一个进程使用”。

尝试:

FileStream file = new FileStream(filename, FileMode.Open);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用新的替换notepad.exe - Replacing notepad.exe with a new one 使用notepad.exe在列表视图中打开活动项目 - Opening active item in a listview with notepad.exe C# 为什么 Process.Start("notepad.exe" myFile) 正常工作,而 Process.Start("notepad++.exe" myFile) 不工作 - C# Why does Process.Start("notepad.exe" myFile) is working and Process.Start("notepad++.exe" myFile) is not working 如何获取notepad.exe的确切路径以便关联文件扩展名 - How to get the exact path of notepad.exe in order to associate a file extension 如何在Windows应用程序中以隐藏模式启动notepad.exe? - How to start notepad.exe in hidden mode in windows application? 使用Awesomium从html按钮运行notepad.exe - Run notepad.exe from a html-button using Awesomium System.Diagnostics.Process.Start(“ Notepad.exe”); 在实时服务器上不工作 - System.Diagnostics.Process.Start(“Notepad.exe”); is not working on live server Process.Start with UseShellExecute 在前一个关闭窗口后无法将 notepad.exe 窗口带到前台 - Process.Start with UseShellExecute fails to bring notepad.exe window to the foreground after a previous close window 如何在 C# windows 应用程序的表单加载事件中的 process.start("notepad.exe") 之前播放 windows 媒体播放器 - how to play windows media player before the process.start("notepad.exe") in form load event in C# windows app SendMessage到Notepad ++打开文件 - SendMessage to Notepad++ to open a file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM