Removing files that were used in C # LibraryStack in Scatterview

I am new to C # / WPF / Surface programming.

I use LibraryStack in ScatterViewItem in ScatterView :

 <Grid Name="DataGrid" Background="LightBlue" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"> <Grid.Resources> <DataTemplate x:Key="LibraryItemTemplate"> <Viewbox Stretch="Uniform"> <Image Source="{Binding}" /> </Viewbox> </DataTemplate> <!-- Styles to ensure each library control uses the above defined templates --> <Style TargetType="{x:Type s:LibraryStack}"> <Setter Property="ItemTemplate" Value="{StaticResource LibraryItemTemplate}"/> </Style> <Style TargetType="{x:Type s:LibraryBar}"> <Setter Property="ItemTemplate" Value="{StaticResource LibraryItemTemplate}"/> </Style> <DataTemplate x:Key="itemTemplate"> <Image Source="{Binding XPath=@FullPath }"/> </DataTemplate> </Grid.Resources> <s:ScatterView HorizontalAlignment="Stretch" VerticalAlignment="Stretch"> <s:ScatterViewItem Name="ScatterViewItem1" Background="DarkGray" MinWidth="800" MinHeight="800" Orientation="0.0" CanRotate="False"> <s:LibraryStack Name="LibraryStack1" Background="Transparent" MinWidth="800" MinHeight="800" AllowDrop="True" > </s:LibraryStack> </s:ScatterViewItem> </s:ScatterView> </Grid> 

I populate the library stack by setting the ObservableCollection to ItemsSource LibraryStack . ObservableCollection consists of strings that are file paths to images.

 ObservableCollection<string> oc = new ObservableCollection<string>(System.IO.Directory.GetFiles(folder)); LibraryStack1.ItemsSource = ocs; 

Now I have a ScatterViewItem with all the images in it with drag and drop.

Then I want to clear all the images from LibraryStack / ScatterViewItem and delete all the files / images in the folder:

 oc=null; LibraryStack1.ItemsSource = null; string[] files = Directory.GetFiles(folder); foreach (String file in files) { try { File.Delete(file); } catch (Exception f) { Console.WriteLine(f); } } 

ScatterViewItem on the screen is empty, but there is always an exception deleting files ( File.Delete(file) ):

System.IO.IOException: The process cannot access the "xyz" file because it is being used by another other process. in System.IO .__ Error.WinIOError (Int32 errorCode, String maybeFullPath) in System.IO.File.Delete (String path) ...

Removing more than FileInfo raises the same exception.

What should I do?

+4
source share
2 answers

Try changing file attributes like this before deleting them.

 File.SetAttributes(file, FileAttributes.Normal); File.Delete(file); 
0
source

I see two possible explanations: -

  • Another process has a file lock (I believe OneDrive often locks my code in order to sync it). Try using Process Explorer to find out what is blocking the file (s)
  • You inadvertently lock files somewhere in your code and lock yourself
0
source

Source: https://habr.com/ru/post/1395374/


All Articles