簡體   English   中英

該進程無法訪問文件“文件名”,因為在刪除文件時它正被另一個進程使用

[英]The process cannot access the file 'filename' because it is being used by another process when deleting a file

當我嘗試刪除文件時,它說我的服務器正在使用它,所以我嘗試實現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;
            }
        }

你試過這種方法嗎? 您必須先從列表中刪除圖像,然后再將其刪除。 由於圖像仍綁定到 slider,因此可能發生錯誤。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM