C# System.IO.__Error.WinIOError(Int32 errorCode, String可能FullPath)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18172913/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
提问by user2468671
Guys can some one tell me why i have such error ....
伙计们可以告诉我为什么我有这样的错误......
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\priv2public 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);
}
}
var stream = File.Open(fsFile, FileMode.Create, FileAccess.Write);
0\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()
Edit:
编辑:
i use my App on windows server 2008 and some files need to read / write permission for my application but i have such error so i need to fix that problem and my source is:
我在 Windows Server 2008 上使用我的应用程序,有些文件需要对我的应用程序进行读/写权限,但我有这样的错误,所以我需要解决这个问题,我的来源是:
##代码##回答by Hans Passant
Where do you close this stream again?
你在哪里再次关闭这个流?
The process cannot access the file xxx because it is being used by another process.
该进程无法访问文件 xxx,因为它正被另一个进程使用。
The Microsoft programmer that wrote this message was a trust-hearty soul. He did not want to assume that you got it wrong. When you are debugging your code, that message should however have ended with "is being used by a process". It includes your own.
撰写此消息的 Microsoft 程序员是一个充满信任的灵魂。他不想假设你弄错了。但是,当您调试代码时,该消息应该以“正在被进程使用”结尾。它包括你自己的。
Also note that you made the same mistake with backupStream. Since you are already using File.ReadAllBytes() to read the file, you might just as well use File.WriteAllBytes() to write it. If you can't afford the delay then you'll need to ensure it is closed in the WriteCompeted callback method.
另请注意,您对backupStream犯了同样的错误。既然您已经在使用 File.ReadAllBytes() 来读取文件,那么您不妨使用 File.WriteAllBytes() 来写入它。如果您负担不起延迟,那么您需要确保它在 WriteCompeted 回调方法中关闭。
If you already do this, then consider that the file might actually be in use by another process. Which doeshappen.
如果您已经这样做了,那么请考虑该文件实际上可能正在被另一个进程使用。这确实发生了。
回答by user3412458
Yes, the very program that gives you this message may be the program that is locking the file. Make sure to practice good housekeeping by closing each data stream after it has been used.
是的,给您此消息的程序可能就是锁定文件的程序。确保通过在使用后关闭每个数据流来进行良好的内务管理。