Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 另一进程正在使用的文件_C#_Wpf_C# 4.0 - Fatal编程技术网

C# 另一进程正在使用的文件

C# 另一进程正在使用的文件,c#,wpf,c#-4.0,C#,Wpf,C# 4.0,当我第一次单击“自定义”按钮时,它工作正常。但当我再次单击时,它会抛出此异常 捕获:“进程无法访问文件'C:\20.jpg',因为它正被另一个进程使用。”(System.IO.IOException) Exception Message=“进程无法访问文件'C:\20.jpg',因为它正被另一个进程使用。”,Exception Type=“System.IO.IOException”您的BitmapImage对象将锁定该文件 只是一个小小的观察,请使用如下方法: private void But

当我第一次单击“自定义”按钮时,它工作正常。但当我再次单击时,它会抛出此异常

捕获:“进程无法访问文件'C:\20.jpg',因为它正被另一个进程使用。”(System.IO.IOException)

Exception Message=“进程无法访问文件'C:\20.jpg',因为它正被另一个进程使用。”,Exception Type=“System.IO.IOException”

您的BitmapImage对象将锁定该文件

只是一个小小的观察,请使用如下方法:

private void ButtonCustomarinfoEditClick(object sender, System.Windows.RoutedEventArgs e)
    {
      ByteToImage(fileName,bytesOfImage,fileSize);
    }   

private ImageSource ByteToImage(string fileName, byte[] bytesOfImage, int fileSize)
    {
        FileStream imageFilestream = new FileStream(fileName, FileMode.Create, FileAccess.Write);
        imageFilestream.Write(bytesOfImage, 0, fileSize);
        imageFilestream.Flush();
        imageFilestream.Close();
        imageFilestream.Dispose();
        BitmapImage myBitmapImage = new BitmapImage();
        myBitmapImage.BeginInit();
        myBitmapImage.UriSource = new Uri(fileName);
        myBitmapImage.DecodePixelWidth = 200;
        myBitmapImage.EndInit();
        return myBitmapImage;
    }
否则,您可能会遇到文件仍在使用的情况(如果在调用Close()方法之前发生异常)。

可能存在重复的文件
using(FileStream imageFilestream = new FileStream(fileName, FileMode.Create, FileAccess.Write)) { 
        imageFilestream.Write(bytesOfImage, 0, fileSize); 
}