繁体   English   中英

C#:如何找出哪个进程打开了一个文件以及如何关闭它?

[英]C#: How do I find out which process opened a file and how can I close it?

我想删除用户根据他的请求选择的文件,但该文件已被另一个进程打开,我得到 System.IO.IOException。 在应用程序开始时,所有文件都通过 SQLite 调用并分配给同时表示 ListView 的 ItemSource 的 ObservableCollection。 现在,例如,显示了图像,我认为该进程使用了这些文件,但该进程不再终止。 如何关闭此进程以重新获得对文件的访问权限?

     // In Constructor
     if (!(Directory.Exists(pathString)))
            Directory.CreateDirectory(pathString);
        using (Process process = Process.GetCurrentProcess())
        {
            GetUris();
        }
      private async void GetUris()
      {
        var db = App.Db;
          //aus datenbank lesen
        if (db.GetAllItemsAsync().Result.Count > 0)
        {
            for (int i = 0; i < db.GetAllItemsAsync().Result.Count; 
        i++)
            {
                int id = db.GetAllItemsAsync().Result[i].Id;
                ItemData data = db.GetItemAsync(id).Result;
                PlayList.Add(data);
                
            }
        } 
     }
     private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
     {
        var item = (ListView)sender;
        
        if (Mouse.RightButton == MouseButtonState.Pressed)
        {
            MessageBoxResult mbr = MessageBox.Show("Soll die Datei gelöscht werden ?", "Action", MessageBoxButton.YesNo, MessageBoxImage.Question);
            if (mbr == MessageBoxResult.Yes)
            {
                string file = (item.SelectedItem as ItemData).ImageSource;
                
                //Here i want to Delete the File in Folder
                
                DeleteItem((item.SelectedItem as ItemData).URI);
                PlayList.Remove(item.SelectedItem as ItemData);
               
            }
        }
        
    }
bool _quit = false;

Thread _thread = new Thread(queue);
void StartThread() {
    _thread.Start();
}

void KillThread() {
    _quit = true;
}

您可以为线程进程设置标志 true 或 false。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM