System.io.ioexception:进程无法访问该文件,因为它正由另一个进程使用。 。 [英] System.io.ioexception: the process cannot access the file because it is being used by another process. .

查看:185
本文介绍了System.io.ioexception:进程无法访问该文件,因为它正由另一个进程使用。 。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做的是创建一个dir观察者,它将观看dir,例如A,并在看到dir A中的文件到目标目录B后,将某个名称的文件复制到另一个文件夹。将文件丢弃到Dir A并且它有效。但是当我在我的本地机器上运行程序并将dir设置为在服务器中监视和目标目录时,它会抛出以下错误。

如何处理它?<​​br />
未处理的异常:System.IO.IOException:进程无法访问文件'\\IP-exapp-101 \ c $ \ WorkDir \WatchedDir \_f $ 12345.dat',因为它正由另一个人使用System.IO上的
.__。Error.WinIOError(Int32 errorCode,String maybeFullPath)

at System.IO.File.InternalCopy(String sourceFileName,String destFileName,Boolean overwrite ,布局checkHost)

在System.IO.File.Copy(String sourceFileName,String destFileName)

at iSpy_File.Program.OnChanged(Object source,FileSystemEventArgs e)in c :\ users \ xuser \documents \visual studio 2015 \Projects\iSpy_File\iSpy_File \Program.cs:第68行

at System.IO.FileSystemWatcher.OnCreated(FileSystemEventArgs e)

在System.IO.FileSystemWa tcher.NotifyFileSystemEventArgs(Int32 action,String name)

在System.IO.FileSystemWatcher.CompletionStatusChanged(UInt32 errorCode,UInt32 numBytes,NativeOverlapped * overlappedPointer)

在System.Threading。 _IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode,UInt32 numBytes,NativeOverlapped * pOVERLAP)



我尝试过:



我试图查看互联网..

我的代码:

Hi, what i am doing is to create a dir watcher which will watch the dir for example "A" and copy the file of certain name to another folder once it sees the file in the dir A to destination dir B. I dropped the file to the Dir A and it worked. but when i ran the program in my local machine and set dir to be watched and destination dir in server, it throws me the following error.
How do i handle it?
Unhandled Exception: System.IO.IOException: The process cannot access the file '\\IP-exapp-101\c$\WorkDir\WatchedDir\_f$12345.dat' because it is being used by another process.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.File.InternalCopy(String sourceFileName, String destFileName, Boolean overwrite, Boolean checkHost)
at System.IO.File.Copy(String sourceFileName, String destFileName)
at iSpy_File.Program.OnChanged(Object source, FileSystemEventArgs e) in c:\users\xuser\documents\visual studio 2015\Projects\iSpy_File\iSpy_File\Program.cs:line 68
at System.IO.FileSystemWatcher.OnCreated(FileSystemEventArgs e)
at System.IO.FileSystemWatcher.NotifyFileSystemEventArgs(Int32 action, String name)
at System.IO.FileSystemWatcher.CompletionStatusChanged(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* overlappedPointer)
at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)

What I have tried:

I tried to look into internet..
my code:

my code:
public static void Run()
{
string[] args = System.Environment.GetCommandLineArgs();
// If a directory is not specified, exit program.
if (args.Length != 3)
{
// Display the proper way to call the program.
Console.WriteLine("Please Provide Source and destination directory: iSpy_Folder.exe <Source dir> <Destination dir>");
return;
}
// Create a new FileSystemWatcher and set its properties.
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = args[1];
/* Watch for changes in LastAccess and LastWrite times, and
the renaming of files or directories. */
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName;
// Only watch text files.
watcher.Filter = "*.*";
//// will track changes in sub-folders as well
watcher.IncludeSubdirectories = true;
// Add event handlers.
// watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.Created += new FileSystemEventHandler(OnChanged);
// watcher.Deleted += new FileSystemEventHandler(OnChanged);
// watcher.Renamed += new RenamedEventHandler(OnRenamed);
// Begin watching.
watcher.EnableRaisingEvents = true;
// Wait for the user to quit the program.
Console.WriteLine("Press \'q\' to quit Program.");
while (Console.Read() != 'q') ;
}
// Define the event handlers.
private static void OnChanged(object source, FileSystemEventArgs e)
{
string[] args = System.Environment.GetCommandLineArgs();
// Specify what is done when a file is changed, created, or deleted.
Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
if ("_F$"== e.FullPath.Substring(e.FullPath.LastIndexOf("\\")+1, 3).ToUpper())
{
if (File.Exists(e.FullPath))
{
File.Copy(e.FullPath, args[2] + "\\" + e.FullPath.Substring(e.FullPath.LastIndexOf("\\")));
Console.WriteLine("File Copied");
}
}
}

推荐答案

\ WorkDir \WatchedDir\_f
\WorkDir\WatchedDir\_f


12345.dat',因为它正被另一个进程使用。

在System.IO .__ Error.WinIOError(Int32 errorCode,String maybeFullPath)
$ System.IO.File.InternalCopy中的b $ b(String sourceFileName,String destFileName,Boolean overwrite,Boolean checkHost)

at System.IO.File.Copy(String sourceFileName,String destFileName)

在c:\users \ xuser \documents\visual studio 2015中的iSpy_File.Program.OnChanged(对象源,FileSystemEventArgs e)2015.\\Projects \ iSpy_File \ iSpy_File \ Programr.c:第68行

在System.IO.FileSystemWatcher.OnCreated(FileSystemEventArgs e)

在System.IO.FileSystemWatcher.NotifyFileSystemEventArgs(Int32 action,String name)

at System.IO.FileSystemWatcher.CompletionStatusChanged(UInt32 errorCode,UInt32 numBytes,NativeOverlapped * overlapp edPointer)

在System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode,UInt32 numBytes,NativeOverlapped * pOVERLAP)



我是什么尝试过:



我试图查看互联网..

我的代码:

12345.dat' because it is being used by another process.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.File.InternalCopy(String sourceFileName, String destFileName, Boolean overwrite, Boolean checkHost)
at System.IO.File.Copy(String sourceFileName, String destFileName)
at iSpy_File.Program.OnChanged(Object source, FileSystemEventArgs e) in c:\users\xuser\documents\visual studio 2015\Projects\iSpy_File\iSpy_File\Program.cs:line 68
at System.IO.FileSystemWatcher.OnCreated(FileSystemEventArgs e)
at System.IO.FileSystemWatcher.NotifyFileSystemEventArgs(Int32 action, String name)
at System.IO.FileSystemWatcher.CompletionStatusChanged(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* overlappedPointer)
at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)

What I have tried:

I tried to look into internet..
my code:
my code:
public static void Run()
{
string[] args = System.Environment.GetCommandLineArgs();
// If a directory is not specified, exit program.
if (args.Length != 3)
{
// Display the proper way to call the program.
Console.WriteLine("Please Provide Source and destination directory: iSpy_Folder.exe <Source dir> <Destination dir>");
return;
}
// Create a new FileSystemWatcher and set its properties.
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = args[1];
/* Watch for changes in LastAccess and LastWrite times, and
the renaming of files or directories. */
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName;
// Only watch text files.
watcher.Filter = "*.*";
//// will track changes in sub-folders as well
watcher.IncludeSubdirectories = true;
// Add event handlers.
// watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.Created += new FileSystemEventHandler(OnChanged);
// watcher.Deleted += new FileSystemEventHandler(OnChanged);
// watcher.Renamed += new RenamedEventHandler(OnRenamed);
// Begin watching.
watcher.EnableRaisingEvents = true;
// Wait for the user to quit the program.
Console.WriteLine("Press \'q\' to quit Program.");
while (Console.Read() != 'q') ;
}
// Define the event handlers.
private static void OnChanged(object source, FileSystemEventArgs e)
{
string[] args = System.Environment.GetCommandLineArgs();
// Specify what is done when a file is changed, created, or deleted.
Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
if ("_F


== e.FullPath.Substring(e.FullPath.LastIndexOf(\\)+ 1,3).ToUpper() )
{
if(File.Exists(e.FullPath))
{
File.Copy(e.FullPath,args [2] +\\+ e.FullPath.Substring(e.FullPath.LastIndexOf( \\)));
Console.WriteLine(文件已复制);
}
}
}
"== e.FullPath.Substring(e.FullPath.LastIndexOf("\\")+1, 3).ToUpper()) { if (File.Exists(e.FullPath)) { File.Copy(e.FullPath, args[2] + "\\" + e.FullPath.Substring(e.FullPath.LastIndexOf("\\"))); Console.WriteLine("File Copied"); } } }


这篇关于System.io.ioexception:进程无法访问该文件,因为它正由另一个进程使用。 。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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