fabschu
fabschu

Reputation: 121

Deleting files which have been used in a C# LibraryStack in a Scatterview

I'm new to C#/WPF/Surface programming.

I'm using a LibraryStack in a ScatterViewItem in a 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 fill the Library Stack by setting a ObservableCollection to the ItemsSource of the LibraryStack. The ObservableCollection consists of strings, which are file paths to images.

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

Now I've got a ScatterViewItem with all images in it with drag and drop.

Then I want to clear all images from the LibraryStack/ScatterViewItem and delete all 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);
  }
}

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

System.IO.IOException: The process cannot access the file 'xyz' because it is being used by another another process. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.File.Delete(String path) ...

Deleting over FileInfo throws the same exception.

What should I do?

Upvotes: 4

Views: 1224

Answers (2)

David Hayes
David Hayes

Reputation: 7512

I see two possible explanations:-

  1. Another process has a lock on the file (I find OneDrive often locks my code to sync it). Try using Process Explorer to see what has a lock on the file(s)
  2. You're inadvertently locking the files somewhere in your code and blocking yourself

Upvotes: 0

Pabinator
Pabinator

Reputation: 1641

Try changing the file attributes like this before deleting them.

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

Upvotes: 0

Related Questions