该进程无法访问该文件,因为该文件正由另一个进程使用.File.Copy

Jon*_*unn 1 c# copy winforms

我正在尝试将png文件复制到我的项目文件夹的一部分,但是当我尝试时,我得到了这个错误.

该进程无法访问文件'....../Image.png',因为它正由另一个进程使用.

我可以向你保证没有其他程序正在访问该文件,我所有的winforms设计器窗口都关闭,以及任何其他应用程序或窗口.

这是我正在使用的代码,我已经在这个网站上尝试了所有其他答案,没有任何作用,任何有关此的帮助将不胜感激!

区域资产导入功能

    private void button_ImportAsset_Click(object sender, EventArgs e)
    {
        // Default to the directory which contains our content files.
        string assemblyLocation = System.Reflection.Assembly.GetExecutingAssembly().Location;
        string relativePath = Path.Combine(assemblyLocation, "../../../../Content");
        string contentPath = Path.GetFullPath(relativePath);

        openFileDialog1.InitialDirectory = contentPath;

        openFileDialog1.Title = "Load Asset";

        openFileDialog1.Filter = "PNG Files (*.png)|*.png|" +
                            "DDS Files (*.dds)|*.dds|" +
                            "BMP Files (*.bmp)|*.bmp|" +
                            "All Files (*.*)|*.*";

        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            // Creates new png version.
            string newFileName = ((openFileDialog1.FileName));

            string filename = openFileDialog1.FileName;

            File.Copy(filename, newFileName, true);

            // Creates new xnb version.
            string outFileName =
            STATIC_CONTBUILDER.BuildXNBFromFile((openFileDialog1.FileName));

            // Copies the asset from the temporary build directory to the assets directory.
            File.Copy(
                Path.Combine(STATIC_CONTBUILDER.contentBuilder.OutputDirectory, outFileName),
                Path.Combine(STATIC_CONTBUILDER.pathToContent, outFileName),
                true);
        }

        Do_Refresh_XNB_Asset_List();
    }

    private void Do_Refresh_XNB_Asset_List()
    {
        listBox_Assets.Items.Clear();
        string[] lst_Files =
        Directory.GetFiles(STATIC_CONTBUILDER.pathToContent, "*.xnb", SearchOption.TopDirectoryOnly);

        for (int i = 0; i < lst_Files.Length; i++)
        {
            listBox_Assets.Items.Add(
                Path.GetFileNameWithoutExtension(
            lst_Files[i]));
        }

    }
    #endregion
Run Code Online (Sandbox Code Playgroud)

Sti*_*tig 12

您似乎正在将文件复制到其上.

string newFileName = openFileDialog1.FileName;

string filename = openFileDialog1.FileName;

File.Copy(filename, newFileName, true);
Run Code Online (Sandbox Code Playgroud)