【发布时间】:2013-10-26 20:35:17
【问题描述】:
我正在尝试将 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
【问题讨论】:
-
该异常消息仅对调试程序的用户准确。当你在开发的时候,它应该是这样的:“你不能访问这个文件,你已经把它打开了,可能忘记使用 Dispose() 方法了。”
-
附注:对于公共样本,尽量避免使用全大写命名约定(大多数 C# 命名指南建议对像 camelCase 这样的标识符进行细微的大小写)。
-
Alexei,我的编程导师让我们以这种方式命名课程。
-
汉斯,我在开发时没有收到错误消息,因为该错误仅在构建代码时出现,并尝试将文件导入我的 winforms 项目,尝试复制它失败然后产生该错误。