c# - System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)

标签 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);
        }
    }

最佳答案

是的,给您这条消息的程序可能就是锁定文件的程序。确保通过在使用后关闭每个数据流来进行良好的管理。

关于c# - System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18172913/

相关文章:

c# - 使用 Xamarin 下载文件

c# - 术语 "Calli"的语义推断或含义是什么?

c# - 如何单次同步两个列表?

c# - 在 WinrRT 应用程序中显示 HTML 内容

c# - 竞争条件/TextWriterTraceListener

c# - 将 Excel 颜色 BGR 转换为 RGB

c# - 你能用速记定义一个 'new' 属性吗?

c# - 通过 C# 将字符串、数字数据写入 Excel 是可行的,但 Excel 无法正确处理数字数据

c# - libcurl .NET 不想在 Win64 上工作

c# - .NET中的RSA加密/解密问题