![](/img/trans.png)
[英]the process cannot access the file 'filename' because it is being used by another process. and Access to the path 'filename' is denied
[英]The process cannot access the file 'C:\PCLtoMove\print.pcl' because it is being used by another process. Windows service
我有一個名為PCLtoMove的文件夾。 我已在此文件夾中應用了filewatcherSystem,將文件從該文件夾移至另一個文件夾。 第一次啟動Windows服務時,它工作正常,但從下一次開始,它將出現異常-
該進程無法訪問文件'C:\\ PCLtoMove \\ fileName.pcl',因為它正在被另一個進程使用。
我要移動文件的代碼是-
private void SavionFileWatcher_Created(object sender, System.IO.FileSystemEventArgs e)
{
try
{
string sourcePath = e.FullPath;
string destination = ConfigurationManager.AppSettings["destination"] + e.Name;
File.Move(sourcePath, destination);
}
catch (Exception ex)
{
this.EventLog.WriteEntry(ex.Message, EventLogEntryType.Information);
}
}
請告訴我我在做什么錯。
通過將以下代碼添加到上述代碼中,我得到了解決方案。 它確認文件已完全移動或創建。
FileStream fs = new FileStream(sourcePath, FileMode.Open, FileAccess.ReadWrite);
fs.ReadByte();
fs.Seek(0, SeekOrigin.Begin);
fs.Dispose();
File.Move(sourcePath,destination);
break;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.