IOException另一个进程使用的文件 [英] IOException File used by another process

查看:74
本文介绍了IOException另一个进程使用的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在使用C#重写部分应用程序。该应用程序启动另一个执行遗留操作的进程。程序。这个遗留程序将

写入日志文件,在它结束之前,它会将特定字符串写入日志文件。


我的原始程序(MKS Toolkit shell)保持运行grep

检查退出字符串的程序)在日志文件上。没有文件共享

问题。


但是,当我使用File.Open或StreamReader打开日志文件时,

引发IOException。我可以看到为什么,因为日志文件打开了

写道。


我确定Toolkit程序如grep等。或C函数,例如

" fopen()"不会遇到这个问题。


请注意,我无法对写入日志的

遗留程序进行任何代码更改持续存档。


我有没有办法在C#中通过这个限制?


谢谢,


-

乔治

Hi,

I am re-writing part of my application using C#. This application starts
another process which execute a "legacy" program. This legacy program writes
to a log file and before it ends, it writes a specific string to the log file.

My original program (MKS Toolkit shell program) which keeps running "grep"
checking the "exit string" on the "log files". There are no file sharing
problem.

However, when I use File.Open or StreamReader to open the log files,
IOException is raised. I can see why, since the log files are open for
writes.

I am certain that Toolkit program such as "grep" or C function such as
"fopen()" will not encounter this problem.

Please note that there is no way for me to make any code changes to the
legacy program which is writing to the log file continuously.

Is there a way for me to by pass this restriction in C#?

Thanks,


--
George

推荐答案



" ;乔治" < WA ** @ nospam.nospam>在消息中写道

新闻:A4 ********************************** @ microsof t.com ...

|

|

|我正在使用C#重写我的部分应用程序。此应用程序启动

|另一个执行遗留的过程。程序。这个遗产计划

写道

|到一个日志文件,在它结束之前,它会将一个特定的字符串写入日志

文件。

|

|我的原始程序(MKS Toolkit shell程序)继续运行grep

|检查退出字符串在日志文件上。没有文件共享

|问题。

|

|但是,当我使用File.Open或StreamReader打开日志文件时,

|引发IOException。我可以看到为什么,因为日志文件打开了

|写道。

|


请发布您的代码。


|我确信Toolkit程序如grep等。或C函数,如

| " fopen()函数"不会遇到这个问题。

|

|请注意,我无法对

|进行任何代码更改正在连续写入日志文件的遗留程序。

|

|有没有办法让我在C#中通过这个限制?

|


这与C#无关,文件共享由操作系统控制

取决于打开时指定的打开和共享模式。


Willy。

"George" <wa**@nospam.nospam> wrote in message
news:A4**********************************@microsof t.com...
| Hi,
|
| I am re-writing part of my application using C#. This application starts
| another process which execute a "legacy" program. This legacy program
writes
| to a log file and before it ends, it writes a specific string to the log
file.
|
| My original program (MKS Toolkit shell program) which keeps running "grep"
| checking the "exit string" on the "log files". There are no file sharing
| problem.
|
| However, when I use File.Open or StreamReader to open the log files,
| IOException is raised. I can see why, since the log files are open for
| writes.
|

Please post your code.

| I am certain that Toolkit program such as "grep" or C function such as
| "fopen()" will not encounter this problem.
|
| Please note that there is no way for me to make any code changes to the
| legacy program which is writing to the log file continuously.
|
| Is there a way for me to by pass this restriction in C#?
|

This has nothing to do with C#, file sharing is controlled by the OS
depending on the open and sharing modes specified during an open.

Willy.


代码:


这是相当通用的,

public System.Boolean ExitCheck(System.IO.DirectoryInfo

OBatchJobDirInfo)

{

System.IO.FileInfo [] logFileArray;

System.String line;

System.Boolean hasExit = false;

System.String pattern = @" SERVER:EXIT";


try

{

logFileArray = oBatchJobDirInfo.GetFiles(" * .log");


foreach(logFileArray中的System.IO.FileInfo oFileInfo)

{


使用(System.IO.StreamReader sr = new

System.IO.StreamReader(oFileInfo.FullName))

{

line = sr.ReadLine();

if(line == null)

{

//文件结尾。

休息;

}


if(System.Text.RegularExpressions.Regex.IsMatch(

line,pattern,

System.Text。 RegularExpressions.RegexOptions.Ignore Case))

{

//退出。

hasExit = true;

break ;

}

}

}


}

catch (例外情况)

{

throw(ex);

}


return hasExit;

}

Code:

It''s fairly generic,

public System.Boolean ExitCheck(System.IO.DirectoryInfo
oBatchJobDirInfo)
{
System.IO.FileInfo[] logFileArray;
System.String line;
System.Boolean hasExit = false;
System.String pattern = @"SERVER: EXIT";

try
{
logFileArray = oBatchJobDirInfo.GetFiles("*.log");

foreach (System.IO.FileInfo oFileInfo in logFileArray)
{

using (System.IO.StreamReader sr = new
System.IO.StreamReader(oFileInfo.FullName))
{
line = sr.ReadLine();
if (line == null)
{
//end of file.
break;
}

if (System.Text.RegularExpressions.Regex.IsMatch(
line, pattern,
System.Text.RegularExpressions.RegexOptions.Ignore Case))
{
//exited.
hasExit = true;
break;
}
}
}

}
catch (Exception ex)
{
throw (ex);
}

return hasExit ;
}

这与C#无关,文件共享由操作系统控制
取决于开放和共享打开期间指定的模式。


是,不。纠正我,如果我错在这里,我认为


操作系统实现

1.基本文件系统功能,如C中的fopen()或fclose()。否

文件锁定。


2.文件锁定功能,直到较新的操作系统引入后才会出现。 (我很老版本的UNIX没有文件锁定

实现。)

3.(可能)文件打开/关闭功能,可以锁定文件

自动。


我认为记事本等程序在打开文件时使用fopen()而没有检查文件锁定。您可以尝试打开锁定状态。文件带

记事本,我相信你仍然可以打开这个锁定的文件。 "写字板"我将假设,C#中的System.IO.File.Open()公开(3),其中文件锁定是 $ b不会这样做。


br />
自动完成。


我在这里问的是,如果在C#中有一个等效的函数/方法,那么

C' 'fopen()?


谢谢,


-

George

Willy Denoyette [MVP]"写道:

乔治 < WA ** @ nospam.nospam>在消息中写道
新闻:A4 ********************************** @ microsof t.com。 ..
|
|
|我正在使用C#重写我的部分应用程序。这个应用程序启动
|另一个执行遗留的过程。程序。这个遗产计划
写道到一个日志文件,在它结束之前,它会将一个特定的字符串写入日志
文件。
|
|我的原始程序(MKS Toolkit shell程序)继续运行grep
|检查退出字符串在日志文件上。没有文件共享
|问题。
|
|但是,当我使用File.Open或StreamReader打开日志文件时,
|引发IOException。我可以看到原因,因为日志文件是为
|打开的写道。
|

请发布您的代码。

|我确信Toolkit程序如grep等。或C等功能,如
| " fopen()函数"不会遇到这个问题。
|
|请注意,我无法对
|进行任何代码更改正在连续写入日志文件的遗留程序。
|
|有没有办法让我在C#中通过这个限制?
|

这与C#无关,文件共享由操作系统控制
取决于开放在开放期间指定的共享模式。

威利。
This has nothing to do with C#, file sharing is controlled by the OS
depending on the open and sharing modes specified during an open.
Yes, and no. Correct me if I am wrong here, I thought

OS implements
1. Basic file system functionality such as fopen() or fclose() in C. No
file locking.

2. File locking functionalities which did not apear until newer OSs are
introduced. (I blieve older version of UNIX did not have file locking
implemented.)
3. (Possibly) File open/close functionalities which does file locking
automatically.

I think program such as notepad uses fopen() when opening a file without
checking against file locking. You can try open a "locked" file with
notepad, and I believe you can still open this locked file. "Wordpad" will
not do the same.

I assume, System.IO.File.Open() in C# exposes (3), where file locking are
done automatically.

What I am asking here is, if there is an equivalent function/method in C# to
C''s fopen()?

Thanks,

--
George
"Willy Denoyette [MVP]" wrote:

"George" <wa**@nospam.nospam> wrote in message
news:A4**********************************@microsof t.com...
| Hi,
|
| I am re-writing part of my application using C#. This application starts
| another process which execute a "legacy" program. This legacy program
writes
| to a log file and before it ends, it writes a specific string to the log
file.
|
| My original program (MKS Toolkit shell program) which keeps running "grep"
| checking the "exit string" on the "log files". There are no file sharing
| problem.
|
| However, when I use File.Open or StreamReader to open the log files,
| IOException is raised. I can see why, since the log files are open for
| writes.
|

Please post your code.

| I am certain that Toolkit program such as "grep" or C function such as
| "fopen()" will not encounter this problem.
|
| Please note that there is no way for me to make any code changes to the
| legacy program which is writing to the log file continuously.
|
| Is there a way for me to by pass this restriction in C#?
|

This has nothing to do with C#, file sharing is controlled by the OS
depending on the open and sharing modes specified during an open.

Willy.



嗨乔治,


感谢您的帖子。


我认为这个问题的原因是您需要打开文件

flag FILE_SHARE_WRITE因为另一个过程已经打开了

写作。这里是MSDN中这个标志的描述:


启用对象的后续打开操作以请求写入

访问。

否则,如果其他进程请求写入

访问权限,则其他进程无法打开该对象。

如果未指定此标志,但该对象已打开以进行写入

访问,函数失败。


在.NET中,我们可以使用FileStream指定FileShare模式:


*没有

*阅读

* ReadWrite

*写


希望这会有所帮助。如果有什么不清楚的话,请随时在这里发帖。


问候,

Walter Wang(wa****@online.microsoft.com,删除' '在线'。')

微软在线社区支持


==================== ==============================

在回复帖子时,请回复群组通过你的新闻阅读器

其他人可以从你的问题中学习并从中受益。

==================== ==============================

此帖子提供按现状 ;没有保证,也没有授予任何权利。

Hi George,

Thank you for your post.

I think the cause of this problem is that you need to open the file with
flag FILE_SHARE_WRITE since another process is already opening the for
writing. Here''s the description of this flag in MSDN:

Enables subsequent open operations on an object to request write
access.
Otherwise, other processes cannot open the object if they request write
access.
If this flag is not specified, but the object has been opened for write
access, the function fails.

In .NET, we can use FileStream to specify a FileShare mode:

* None
* Read
* ReadWrite
* Write

Hope this helps. Please feel free to post here if anything is unclear.

Regards,
Walter Wang (wa****@online.microsoft.com, remove ''online.'')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.


这篇关于IOException另一个进程使用的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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