4

我是 C#/WPF/Surface 编程的新手。

我正在使用 a LibraryStackin ScatterViewItema 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>

我通过将 a设置ObservableCollection为. 由字符串组成,这些字符串是图像的文件路径。ItemsSourceLibraryStackObservableCollection

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

现在我有了一个ScatterViewItem包含所有图像的拖放功能。

然后我想清除LibraryStack/中的所有图像ScatterViewItem并删除文件夹中的所有文件/图像:

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 是空的,但通过删除文件(File.Delete(file))总是抛出异常:

System.IO.IOException:该进程无法访问文件“xyz”,因为它正被另一个进程使用。在 System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) 在 System.IO.File.Delete(String path) ...

删除FileInfo会引发相同的异常。

我应该怎么办?

4

2 回答 2

0

在删除它们之前尝试像这样更改文件属性。

File.SetAttributes(file, FileAttributes.Normal);
File.Delete(file);
于 2014-06-23T18:49:03.357 回答
0

我看到两种可能的解释:-

  1. 另一个进程锁定了文件(我发现 OneDrive 经常锁定我的代码以同步它)。尝试使用Process Explorer查看文件上的锁定内容
  2. 您无意中将文件锁定在代码中的某个位置并阻止了自己
于 2014-06-23T18:54:02.550 回答