[c#] Unable to copy file - access to the path is denied

I am using Visual Studio 2005. After taking code from version control first, the c#.net application runs correctly. But, after doing some modifications, when I build I am getting the following error:

Error 383 Unable to copy file "..\root\leaf\Bin\Debug\test.Resources.xml" to "Bin\Debug\test.Resources.xml". Access to the path 'Bin\Debug\test.Resources.xml' is denied. li.rollmodel

Does anybody know why this issue is occurring?

Edit I can see that my entire project source code folder is Read-only, and I am not able to remove the read-only property.

First, can anybody tell me how to remove the Read-only property for this folder? I have tried removing it but the read-only property persists. I tried from the version-control side also and that did not work, either.

This question is related to c# visual-studio-2005 access-denied

The answer is


So I just ran into the same issue, the cause of mine, I had my development folder shared so I could use a mac as a build host for an IOS application using Xamarin. The project was running on mac which took ownership of the dll therefore I could not make changes to that dll from anywhere else. Simply stopping the application on the mac returned ownership back to me which allowed full access again. I hope this make since.


For me the issue was that another project in the solution had the same .dll file which had been checked in more recently.

Manually copying the more recently checked in version of the .dll from the other project bin folder to bin folder in the project that was generating the error fixed the problem.

A longer term fix for our solution would be to use the same exact .dll for all projects in the solution instead of having the same .dll file in multiple projects but I didn't feel like tackling that so the quick-and-dirty solution was to just copy over the newer version.


I stepped into this problem too.

First go and check if you have mapped your bin and obj folder to the Source Control program.

This may be turning your files from binaries folders into read only archives, which make impossible to visual studio to overwrite them when it compiles the code.

Go and remove the mapping from these folders, check the changes and try again.

My problem occurred using TFS(Team foundation server) and Visual Studio 2010.

Hope this helps someone.


I resolved this issue myself. The problem was I had the solution opened in another place. After closing it it works


In my case it was the antivirus which blocked the file.


Simple Solution:

Just upgrade the following packages

Microsoft.CodeDom.Providers.DotNetCompilerPlatform v1.0.5 to v1.0.7

It will resolve the issue.


I had the same issue, To resolved this i had checked/done several things

What Didn't work

1) Read/write permissions were given to directory 
2) Restarted visual studio
3) Tried deleting visual studio temp files

What worked at last

Just paste the command given by Fred Morrison into the "Package Manager Console":

Get-Process | Where-Object -Property Name -EQ 'VBCSCompiler' | Stop-Process -Force -Verbose

Note: Just an observation Restarting Visual studio might not work in this case but restarting system can because stoppin process "VBCSCompiler" was the solution so we can do it either way.


1) close the visual studio solution

2) navigate to command prompt --> run as administrator--> iisreset /stop

3) navigate to c--> Windows --> Microsoft.Net --> Framework64--> v4.030319--> Temporary Asp.NET Files --> Delete all the files and folders in this path.

4) Navigate back to command prompt --> iisreset /start

5) Now open the visual studio --> run as administrator --> clean the solution and build it(don't rebuild..just build worked for me)


I created this problem when I added a new setup project to the solution, and then added files directly from the /bin/release folder of the main application project to the application files folder of the setup project. The source control of the setup project was consistently blocking me from completing a build of the main application project.

Solution: create a separate dump folder outside of any of the projects that will contain all the files to be included in the install, and add them from there. It's a pain because now I have to remember to copy all the files for each new install package. I might see if I can do something with post-build actions our an automated build to make the process smoother.


If someone wants an extra simple solution before trying anything else.

Simply restart your PC. Open Visual Studio, try to build again.


I was able to resolve the problem by removing the target file which is complaining(in your example "Bin\Debug\test.Resources.xml") from bin folder of target web site and re build it.That fixed it for me.


I solved this problem: Close Visual Studio, open it again and load the solution, Rebuild your solution. My problem occurred using TFS and VIsual Studio 2010.


Just right click on your MVC project and click on the clean option. I had a similar problem and cleaning the project before rebuilding resolved it for me.


Run your Visual Studio as Administrator


Just make sure that the folder is NOT Read-Only and rebuild the solution


I also had this issue. Here's how is resolved this

  • Exclude bin folder from project.
  • Close visual studio.
  • Disk cleanup of C drive.
  • Re-open project in visual studio.
  • And then rebuild solution.
  • Run project.

This process is works for me.


Make sure you append the file name after the path name (path\fileName.extension). I spent more than one hour without note this.


I had also the same problem. I fixed it by unchecking the Read-only properties of root folder.


Old post, but this zombie is hitting VS 2017 (I haven't dug into why it's just "some" projects). In this case, it's not user permissions, rather IIS Express process is still using the files.

You'll see the icon in your task tray IIS Express icon

  1. Right Click
  2. Exit
  3. You should be able to rebuild without this annoying "permission denied" message.

This is also why "restarting Visual Studio" will "fix" the issue. Doing so, stops IIS Express.

Hth...


Kill process VBCSCompiler.exe and rebuild.


I re-added all of my non-.NET dependencies / references and it did the trick.


I had the same error but I am using Perforce version control. Here's how I fixed it.

  1. Closed Perforce P4V client
  2. Restarted Visual Studio 2010 (might not be necessary)
  3. Rebuilt the project, which succeeded
  4. Felt exceptionally happy and disgusted at the same time

I also had same problem. I got error messages that related to cannot copy since access to path denied. In my case all my dll's and xml files and so on are place at D:\TFS\Example\Bin\Debug folder.

I right clicked on Bin folder and clicked Properties and saw that Read-only check box is checked under Attributes.

I un-checked Read only check box and cliked Apply and clicked OK on the new popup that is shown.

I went back to Visual Studio and build my solution which was giving me error messages.

Voilaa.. This time it build successfully without errors.

I donot know whether this is perfect but I did this to solve my issue.


Most of the answers I have seen are objectively exploring possible issues with the environment, which is probably correct for 99% of the people running into this issue (unable to copy file from ... to ... access is denied).

I thought I should share my experience for the 1% who ran into this problem due to misc reasons.

I wrote a batch file renaming program that i use to act on tens of thousands of files, and my AntiVirus interpreted it as trojan and auto quarantined it. Having this path sit in my AntiVirus' blacklist, visual studio can never copy the *.exe into the bin folder, hence resulting unable to copy .exe.

My resolution is to whitelist this path and the issue is resolve.

Cheers.


Clear all referenced libraries in "bin\debug" and right click to Solution in solution explorer after click to "Clean Solution".

And Rebuild!!


This can be a strange by-product of loading source initially from source control (TFS etc.) where all files and folders are write protected. Go to the top level directory and check the properties and you could find the Read-only attribute checked. Uncheck that and the system should ask you if you want all the dirs under the top level to be set the writable. This should fix it.


I know its an old thread but for those looking for answers, like me a few minutes ago, I recommend trying to restart your computer first. That alone fixed for me. Before could not even manualy copy to the folder.


Go to file's path then un-check read only checkbox of this file.


In my case, I ported a project from Windows to OSX, using Visual Studio Community 7.1.5 for Mac. What did the trick was disabling the Use MSBuild engine (recommended for this type of project)option on the project preferences:

enter image description here


If you copy any files across to a solution, make sure the files are not in Read Only mode. Right click on file and uncheck the attribute option solved my problem.


I tried everything here, somehow deleting all the bin and obj folders in my project didn't work. Releasing the folders from other processes did nothing. Restarting did nothing. Removing read only from every file by unchecking it on the main folder didn't help.

Every dependency ended up adding themselves to the problem. Building directly in the dependencies' folder worked so I was confused. I was going to duplicate this question but I found something that worked.

You know what worked?

dotnet clean


Had the same problem, but restarting Visual Studio every time was no option for me, as the issue occurs sometimes very often.

I handled it by installing Unlocker (tries to install any toolbar at installation, so don't forget to uncheck this), this application gives me fast access to rename/delete a locked ".xml"-File. I know that this only a workaround too, but for me it was the fastest solution to solve this problem.


Can any body know why this issue is coming?

Looking at your response that you resolved your problem by manual copying, I would say that the code you were working on was made by some other user (with administrator privileges too) so it was locked to you. By performing copy --? paste, you made your OWN copy of the source with all access you required. The only thing to notice is that, in this case if this other developer will need to work on your copy, he/she will jump in pretty same problem you got before.


This has reared it's head again in Visual Studio 2017, in this case the cause is the Application Insights process ServiceHub.DataWarehouseHost.exe.

There is a workaround discussed in the thread warning MSB3026: Could not copy "obj\Debug\netcoreapp1.1\src.pdb" to "bin\Debug\netcoreapp1.1\src.pdb", which is to add a pre-build event to the project to kill the process every time the project is built. Quoting from that link:

  • Right click properties on project
  • Choose properties
  • Build Events
  • Pre-build event command line
taskkill /IM ServiceHub.DataWarehouseHost.exe /F 2>nul 1>nul
Exit 0
  • Save and build

You aren't supposed to change the folder attribute to not-readonly. The reason you are seeing this error message is that source control assumes that you only store your miscellaneous files somewhere other than bin folder, for it it reserved for files that are automatically created by .Net and it doesn't want to add them to source control.

I suggest instead of using Environment.CurrectDirectory (which I assume you are using currently), you create a folder named "MyProjectName" in %appdata% address and then use:

System.IO.Path.Combine(Environment.GetEnvironmentVariable("appdata"),"YourProjectName").


Check Task Manager and make sure you don't have a devenv.exe process hanging. Kill the runaway process and try again.


I use Visual Studio 2013. I faced this problem 2 times:

  1. On first occasion, I was running Visual Studio without Administrator rights. So, I closed VS and started it using 'Run as administrator' option. This solved my problem.

  2. On second occasion, I restarted VS many times, every time making sure that I am running it as an administrator. Also, I rebuilt solution many times. But, in spite of that I was getting error. After that, I removed the concerned file from the target location (the file was already present may be from the previous build at the location where it tries to copy to) and rebuilt the solution. After that, error went away and everything ran smoothly!


Changing the output path worked for me in Visual Studio 2015. This should help - Changing the Build Output directory


First go to location of the file. Then right click on the folder of the file -> Properties -> Unchecked Read Only Option and apply to files and its sub folders. It resolved my issue. Happy Coding!


I ran into this issue multiple times, and the solution which I found is to delete the debug folder and then rebuild your solution/project. Worked for me!!


I tried to log off and log back in and it worked for me. Hopefully this will help someone else.


I solved this problem by deleting the contentious files from bin folder and rebuilding the project.


Examples related to c#

How can I convert this one line of ActionScript to C#? Microsoft Advertising SDK doesn't deliverer ads How to use a global array in C#? How to correctly write async method? C# - insert values from file into two arrays Uploading into folder in FTP? Are these methods thread safe? dotnet ef not found in .NET Core 3 HTTP Error 500.30 - ANCM In-Process Start Failure Best way to "push" into C# array

Examples related to visual-studio-2005

How to generate List<String> from SQL query? Unable to copy file - access to the path is denied How do I print to the debug output window in a Win32 app? How to debug a referenced dll (having pdb) What is the difference between Release and Debug modes in Visual Studio? The name 'controlname' does not exist in the current context How do I programmatically get the GUID of an application in .NET 2.0 Cannot find Dumpbin.exe How to fix "Referenced assembly does not have a strong name" error? ERROR : [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified

Examples related to access-denied

pip install access denied on Windows Getting "java.nio.file.AccessDeniedException" when trying to write to a folder mysqldump Error 1045 Access denied despite correct passwords etc Hosting ASP.NET in IIS7 gives Access is denied? MySQL ERROR 1045 (28000): Access denied for user 'bill'@'localhost' (using password: YES) Access to the path 'c:\inetpub\wwwroot\myapp\App_Data' is denied Access restriction: Is not accessible due to restriction on required library ..\jre\lib\rt.jar IIS Express gives Access Denied error when debugging ASP.NET MVC Access to the path denied error in C# Unable to copy file - access to the path is denied