复制文件“文件在另一个进程中打开”

时间:2016-09-17 16:38:15

标签: c# wpf windows

我正在制作一个WPF C#应用程序,我需要复制一个文件。正如标题所说,每当我选择任何文件时,它都表示它在另一个进程中打开,所以我猜测它与我的代码有关。

我的文件复制代码

 public static void CopyFile(string file, string destination, ProgressBar progressCallback)
    {
        try
        {
            if (!Directory.Exists(destination)) Directory.CreateDirectory(destination);
        } catch (IOException e)
        {
            MessageBox.Show("There was an error while creating the resource packs directory " + e.Message, "Canno't create directory", MessageBoxButton.OK);
            return;
        }

        int halfAMeg = (int)(1024 * 1024 * 0.4);

        FileStream strIn = null;
        FileStream strOut = null;

        try
        {
            strIn = new FileStream(file, FileMode.Open);
            strOut = new FileStream(Path.Combine(destination, file), FileMode.Create);
        }
        catch (IOException e)
        {
            MessageBox.Show("There was an error while copying the file: " + e.Message, "Canno't copy file", MessageBoxButton.OK);
            return;
        }

        byte[] buf = new byte[halfAMeg];
        while (strIn.Position < strIn.Length)
        {
            int len = strIn.Read(buf, 0, buf.Length);
            strOut.Write(buf, 0, len);

            progressCallback.Maximum = Int32.MaxValue;
            progressCallback.Value = (int)(Int32.MaxValue / (strIn.Position / strIn.Length));
        }
        strIn.Close();
        strOut.Close();
    }

非常感谢任何帮助。

0 个答案:

没有答案