Mike Pateras
Mike Pateras

Reputation: 15015

What could prevent VS 2008 from deleting a file that I can delete manually?

I'm trying to build a project using Visual Studio 2008, but I'm getting this error:

Could not delete file '<filename>'. Make sure that the file is not open by another process and is not write-protected.

The file is most definitely not write protected (it was, but I changed it), and I don't think it's in use by some other process because I can delete it without a problem in windows explorer.

What might prevent Visual Studio from deleting it?

Upvotes: 0

Views: 943

Answers (1)

Winter Dragoness
Winter Dragoness

Reputation: 1325

I fixed this error as follows:

  1. Select all projects in the Solution Explorer on the left.
  2. Right click any of the selected projects and choose "Properties"
  3. Choose "All Configurations" from the Configuration pulldown menu.
  4. Choose "All Platforms" from the Platform pulldown menu.
  5. Pick "Configuration Properties" on the left, then "General".
  6. Edit the "Output Directory" entry. By default it has a value like "$(SolutionDir)$(PlatformName)\$(ConfigurationName)". Change this to point to a directory above $(SolutionDir). Say your $(SolutionDir) is "\Code\$(ProjectName)", try setting "Output Directory" to "\Code\Build\$(ProjectName)\$(PlatformName)\$(ConfigurationName)"
  7. Edit the "Intermediate Directory" entry and set it to "$(OutDir)". This puts intermediate files in the same place as the "Output Directory" entry. This step might be optional, but it's how I have it set.

I'm not sure why VC2008 has this problem but I'm guessing that it has a separate thread that's always scanning files in $(SolutionDir) and randomly preventing them from being deleted. When I had Output Directory within $(SolutionDir) I would randomly get errors trying to delete files during build and it seemed it could happen on any file. My builds would fail as a result about 90% of the time. Running VC as Administrator as suggested in comments below the original question did not fix the problem for me, nor did replacing all file permissions, running chkdsk /F, or rebooting. I would also theorize that this problem becomes gradually worse as your project becomes larger and more complex.

Upvotes: 1

Related Questions