无法打开另一个进程使用的文件 - [英] Cannot open a file used by another process -

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

问题描述

我有一个C#程序,读入一个大的文本文件。旧版本的文件使用了一些VBNet调用。 。 。

  ff = VBNET.FileSystem.FreeFile(); 
VBNET.FileSystem.FileOpen(ff,sPath,VBNET.OpenMode.Input,VBNET.OpenAccess.Default,VBNET.OpenShare.Default,-1);
while(!(VBNET.FileSystem.EOF(ff)))//)开始Do直到EOF(tf);
{
VBNET.FileSystem.Input(ff,ref sMyString);
。 。 。

这些都是过时的,通过将逗号解释为EOL而导致问题,因此我决定用System替换它们。 IO调用。 。 。

  System.IO.File.Open(sPath,System.IO.FileMode.Open,System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite); 
System.IO.StreamReader file = new System.IO.StreamReader(sPath); ((sMyString = file.ReadLine())!= null)
{
。 。 。

但是我得到进程无法访问文件C:\ Users \Peter\WorkAtHome\\\
Chain.txt,因为它被另一个进程使用。

但是在使用VBNet调用的代码的旧版本中我没有看到这个错误。而我不记得在记事本,我可以读它并写下来!而且我没有证据显示这个文件正在被另一个进程使用。我基于我的语法的答案读取另一个进程使用的文件 - 即使这种情况下,它实际上是由另一个进程使用(即,我认为错误是虚假的)。 (所以不要把这个标记为重复的那个)我在做什么错了?

解决方案

  System.IO.File.Open(sPath,System.IO.FileMode.Open,System.IO.FileAccess.Read,System.IO.FileShare.ReadWrite); 
System.IO.StreamReader file = new System.IO.StreamReader(sPath);

这里的问题可能是因为第一行(File.Open)已经在打开文件时您正尝试在第二行中使用StreamReader(sPath)再次读取它。



您可以试试

<$ p $ (FileStream fs = System.IO.File.Open(sPath,System.IO.FileMode.Open,System.IO.FileAccess.Read,System.IO.FileShare.ReadWrite))
$ b {
using(System.IO.StreamReader filesr = new System.IO.StreamReader(fs))
{
//从streamreader读取
}
}

请注意,由 File.Open 创建的FileStream对象被传递到 StreamReader


I have a C# program that reads in a big text file. The old version of the file used some VBNet calls . . .

  ff = VBNET.FileSystem.FreeFile();
  VBNET.FileSystem.FileOpen(ff, sPath, VBNET.OpenMode.Input, VBNET.OpenAccess.Default, VBNET.OpenShare.Default, -1);
  while (!(VBNET.FileSystem.EOF(ff)))   //  )start       Do Until EOF(tf);
  {
      VBNET.FileSystem.Input(ff, ref sMyString);
. . .

which were archaic and causing problems by interpreting commas as EOL's, so I decided to replace them with System.IO calls . . .

        System.IO.File.Open(sPath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite); 
        System.IO.StreamReader file = new System.IO.StreamReader(sPath);
        while ((sMyString = file.ReadLine()) != null)
        {
     . . . 

But I'm getting "The process cannot access the file 'C:\Users\Peter\WorkAtHome\nChain.txt' because it is being used by another process." But I don't get that error in the old version of the code that used the VBNet calls. And I don't get it in Notepad where I can read it and write it! And I have no evidence the file is actually being used by another process. I based my syntax on the answers to Reading a file used by another process - even though it that case it actually was being used by another process (i.e, I think the error is spurious). (so don't flag this as a duplicate of that one) What am I doing wrong?

解决方案

System.IO.File.Open(sPath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite); 
System.IO.StreamReader file = new System.IO.StreamReader(sPath);

The problem here could be because the first line (File.Open) is already keeping the file open when you are attempting to read it again in second line using StreamReader(sPath)

Could you please try

using(FileStream fs = System.IO.File.Open(sPath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite))
{
    using(System.IO.StreamReader filesr = new System.IO.StreamReader(fs))
    {
        //read from streamreader
    }
}

Please note here that the FileStream object created by File.Open is passed on to the StreamReader.

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

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