【问题标题】:The process cannot access the file 'filename' because it is being used by another process when deleting a file该进程无法访问文件“文件名”,因为在删除文件时它正被另一个进程使用
【发布时间】:2021-06-08 14:31:27
【问题描述】:

当我尝试删除一个文件时,它说我的服务器正在使用它,所以我尝试实现using (Image img = Image.FromFile(imgFilePath)),但随后我收到了parameter is not valid 错误消息。我也尝试将图像存储在一个变量中,然后调用.Dispose(),但这也不起作用。

任何帮助将不胜感激!

删除代码(客户端):

private void pbDelete_Click(object sender, EventArgs e)
        {
            if (imgSliderCapture.Images.Count > 0)
            {
                int currentImageIndex = imgSliderCapture.CurrentImageIndex;
                m_DispenseOrderPresenter.DeleteCapturedRxImage(currentImageIndex);
                imgSliderCapture.Images.RemoveAt(currentImageIndex);

                if (imgSliderCapture.Images.Count <= 0)
                {
                    pbCamera.Image = global::KeyCentrix.RxKey.UI.Properties.Resources.white_camera;
                    pbCamera.BackColor = Color.DarkGray;
                    imgSliderCapture.ImageList = null;
                    pbCamera.Padding = new Padding(38, 15, 40, 35);
                    pbCamera.Size = new Size(169, 123);
                    pbDelete.Visible = false;
                }
            }
        }

public void DeleteCapturedRxImage(int currentImageIndex)
        {
            if (File.Exists(m_PathImagePairs[currentImageIndex].Key))
            {
                File.Delete(m_PathImagePairs[currentImageIndex].Key);
            }
        }

我认为可能导致问题的服务器端代码:

public List<KeyValuePair<string, Image>> GetCapturedRxImages(string rxId)
        {
            try
            {                 
                string[] imgFilePaths = GetCapturedRxImagesFilePaths(rxId);
                List<KeyValuePair<string, Image>> images = new List<KeyValuePair<string, Image>>();
                List<byte[]> byteArrays = new List<byte[]>();

                if (imgFilePaths == null || imgFilePaths.Length < 1)
                {
                    return null;
                }

                foreach(string imgFilePath in imgFilePaths)
                {
                    images.Add(new KeyValuePair<string, Image>(imgFilePath, Image.FromFile(imgFilePath)));
                }
                return images;
            }
            catch (Exception ex)
            {
                LogFactory.LogEvent(typeof(RxDirSvc), LogLevel.Error, String.Format("Exception in {0}", MethodInfo.GetCurrentMethod()), ex);
                return null;
            }
        }

        /// <summary>
        /// Gets the user captured drug's image file name and path
        /// </summary>
        /// <param name="identifier"></param>
        /// <returns></returns>
        public string[] GetCapturedRxImagesFilePaths(string rxId)
        {
            try
            {
                List<string> filesFound = new List<string>();
                string pathFolder = Directory.GetParent(System.Windows.Forms.Application.StartupPath) + @"\images\RxCapturedImages\" + rxId;
                
                filesFound.AddRange(Directory.GetFiles(pathFolder, "*.jpeg"));
                return filesFound.ToArray();
            }
            catch (Exception ex)
            {
                LogFactory.LogEvent(typeof(RxDirSvc), LogLevel.Error, String.Format("Exception in {0}", MethodInfo.GetCurrentMethod()), ex);
                return null;
            }
        }

【问题讨论】:

  • 客户端应用程序是否也在服务器上运行?服务器是否对其加载的这些图像进行任何处理,或者将加载然后作为字节数组工作以满足它的需要?
  • @CaiusJard 是的,应用程序在服务器上运行,但服务器不对图像进行任何处理,只是将图像列表作为键值对抓取并返回。将图像加载为字节数组有助于防止错误吗?还是这样我可以使用 using 语句?
  • 任何时候你使用 Image.FromFile 文件都会被锁定,直到图像被处理,但你似乎是说你只是加载图像并将它们发送给客户端然后我会在服务器端说,您绝对应该只加载字节并发送它们;如果要从它们中制作图像,则取决于客户

标签: c# image ioexception


【解决方案1】:

你试过这种方式吗?您必须先从列表中删除图像,然后再将其删除。该错误可能是由于图像仍绑定到滑块而发生的。

imgSliderCapture.Images.RemoveAt(currentImageIndex);
m_DispenseOrderPresenter.DeleteCapturedRxImage(currentImageIndex);

【讨论】:

  • 对不起,我忘记在原帖中添加了!我也确实尝试过,但不幸的是,它似乎仍然出错
猜你喜欢
  • 1970-01-01
  • 2015-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-10
相关资源
最近更新 更多