System.IO .__ Error.WinIOError(Int32 errorCode,String maybeFullPath)

时间:2013-08-11 14:20:41

标签: c#

伙计们可以告诉我为什么会有这样的错误......

2013-08-11 18:44:28 - NPMessage: DEBUG: Dispatching a RPCStorageWriteUserFileMessage
2013-08-11 18:44:28 - RPCStorageWriteUserFileMessage: INFO: Got a request for writing 8192 bytes to file iw4.stat for user alhpons.
2013-08-11 18:44:28 - ProfileData: INFO: Handling profile update request for alhpons
2013-08-11 18:44:28 - ProfileData: ERROR: Exception: System.IO.IOException: The process cannot access the file 'D:\IW4M\NpServer\data\priv2\00\000\alhpons\iw4.stat' because it is being used by another process.
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
   at System.IO.File.ReadAllBytes(String path)
   at NPx.ProfileData.Handle(UpdateRequest request)
   at NPx.ProfileData.Run()

编辑:

我在Windows Server 2008上使用我的应用程序,有些文件需要读取/写入我的应用程序的权限,但我有这样的错误,所以我需要解决这个问题,我的来源是:

public override void Process(NPHandler client)
    {
        var fileName = Message.fileName;
        var fileData = Message.fileData;
        var npid = (long)Message.npid;
        var fsFile = StorageUtils.GetFilename(fileName, npid);

        _client = client;
        _fileName = fileName;
        _npid = npid;

        if (!client.Authenticated)
        {
            ReplyWithError(1);
            return;
        }

        if (client.NPID != (long)npid)
        {
            ReplyWithError(1);
            return;
        }

        if (!Directory.Exists(Path.GetDirectoryName(fsFile)))
        {
            Directory.CreateDirectory(Path.GetDirectoryName(fsFile));
        }

        // are we allowed to write this type of file?
        if (!_fileHooks.ContainsKey(fileName))
        {
            ReplyWithError(1);
            return;
        }

        string backupFile = null;

        int result = _fileHooks[fileName](fileData, fsFile, out backupFile);

        if (result > 0)
        {
            ReplyWithError(result);
            return;
        }

        Log.Info(string.Format("Got a request for writing {0} bytes to file {1} for user {2}.", fileData.Length, fileName, npid.ToString("X16")));

        try
        {
            var stream = File.Open(fsFile, FileMode.Create, FileAccess.Write);

            stream.BeginWrite(fileData, 0, fileData.Length, WriteCompleted, stream);

            if (backupFile != null)
            {
                var backupStream = File.Open(backupFile, FileMode.Create, FileAccess.Write);

                backupStream.BeginWrite(fileData, 0, fileData.Length, BackupWriteCompleted, backupStream);
            }
        }
        catch (Exception ex)
        {
            Log.Error(ex.ToString());
            ReplyWithError(2);
        }
    }

2 个答案:

答案 0 :(得分:2)

是的,给你这条消息的程序可能是锁定文件的程序。确保在使用完每个数据流后关闭每个数据流,以实现良好的内务管理。

答案 1 :(得分:1)

  var stream = File.Open(fsFile, FileMode.Create, FileAccess.Write);

你又在哪里关闭这个流?

  

该进程无法访问文件xxx,因为它正由另一个进程使用。

写这封邮件的微软程序员是一个信任的心灵。他不想以为你弄错了。在调试代码时,该消息应该以“正在被进程使用”结束。它包括你自己的。

另请注意,您使用 backupStream 犯了同样的错误。由于您已经在使用File.ReadAllBytes()来读取文件,因此您也可以使用File.WriteAllBytes()来编写它。如果你负担不起延迟,那么你需要确保它在WriteCompeted回调方法中被关闭。

如果您已经这样做了,那么请考虑该文件实际上可能正由另一个进程使用。哪个发生