Could not write to output file -- Access is denied - c#

I recently switched computers and copied all my projects over to my new local drive. I reformatted the computer I'm on now so it was pretty much a clean machine. Everything seemed to be working fine, but when I opened one of my projects that I had been running from my old machine, it would no longer compile, and I get the following error message:
Could not write to output file 'c:\Users\user\Documents\Projects\RegressionWeb\OnetouchUpload\obj\debug\OneTouchUpload.dll' -- 'Access is denied.'
I'm getting an error like that for each project I have in my solution. I'm also getting this error:
Unexpected error creating debug information file 'c:\Users\user\documents\projects\RegressionWeb\RegressionWeb\obj\Debug\RegressionWeb.PDB' Access is denied
I've searched high and low, and the only similar issues I could find online related specifically to ASP.NET and IIS, neither of which has anything to do with my project (My projects are class libraries of mostly NUnit tests with some support classes).
I am the administrator on my local machine. I have already taken ownership of every file in the project using takeown /f .\RegressionWeb /r /d y and also tried to ensure that nothing had a status of Read-only, but the following file threw an error when I tried changing the read-only property of it's parent folder:
An error occurred applying attributes to the file:
C:...\Regressionweb.sln.DotSettings.user
Access is denied
Basically this project was working perfectly and had no errors from my old computer. After copying over everything, this DotSettings.user file will not be modified, and Visual Studio can't write to any of the dll files. I'm sure the answer must lie in a Visual Studio setting somewhere. Any and all suggestions would be greatly appreciated.

I think Karl has it right. I had a similar situation once, and what I did was delete the file in question, clean the solution, and then rebuild. If the project needs that file it should recreate it; in my case it didn't need it because it never recreated it, but the project ran just fine.
One other thing I would recommend; you may be selecting the read-only attribute of the parent folder off and selecting this setting to recurse through all sub-folders and files, but that doesn't mean that's what's happening. For all your sub-folders and files (especially the ones that are cropping up as errors) inspect each one individually and make sure the read-only attribute is off.

My money is on your files are read-only. Verify they are read-only and change them or add your files to a source control system and let them get handled by that.

I was able to get this working by closing Visual Studio and then opening it again, but being careful to run Visual Studio "As Administrator". To do this, from the Start menu, right click on Visual Studio and choose "Run as Administrator".

it seems that the same error is sometimes displayed when the app pool user doesn't have access to the %TMP%/%TEMP% folder.
You'll need to grant IIS_IUSRS read and modify access over the temp folder of the user the app pool is running as.
This could either be the temp folder in the app pool user's profile, e.g. c:\Windows\ServiceProfiles\NetworkService\AppData\Local\Temp, or the system temp folder at c:\windows\temp.

See if it is related to this:Could not write to output file - Access is denied
. That would be a bit of a bugger to track down. In this case the project is self-referencing the dll and preventing access to the file.

This has a very simple solution, you just have to make sure that your directory name(folder name) is not the same as your file name. I created a folder by the name Pointers, my code was in a file pointers.cpp. When I compiled the code it kept showing the same error. I just had to change the directory or folder name to L1_pointers. You can change it to anything you want and it worked.

Related

C# Directory.GetDirectories() return inexisting directory

I want to use Directory.GetDirectories(path) and then working with sub-directories but, when I start running my app, VS return me System.UnauthorizedAccessException. I can understand I fix this, no problem. The real problem is, as you can see in the linked picture, code try to acces a folder that doesn't exist.
In windows explorer I show all hide files and folder, and as you can see (again) when I go to the right spot in my PC, "Ma Musique" doesn't exist (and never existed by the way).
Where is folder come from and how can I deal with this ?
EDIT :
Code try to access this directory just after Lumion directory. As you see above, "Ma Musique" doesn't exist.
CMD (as administrator) doesn't find it too.
Directory.Exists() return true when give it "Ma musique" directory path.
That is what is known as a "virtual" folder and it is there for backwards compatibility with old versions of Windows. When users got their own music/video/etc folders, they were all turned into system virtual folders that don't actually exist at all as they are aggregates of content in multiple folders. However programs that expect these folders to exist would break if they were missing so Windows would make a symbolic link that in some cases would allow a redirect. I don't remember for sure but i think the redirect only worked if you were running as administrator. In any case you can't browse into them as they aren't real.
You are seeing "acces denied" and/or do get this Exception "System.UnauthorizedAccessException".
Microsoft has a tool ICACLS that displays or modifies discretionary access control lists (DACLs) on specified files, and applies stored DACLs to files in specified directories.
Because of the mentioned error, you should investigate the response of:
icacls "C:\Users\thoma\Documents\Ma musique"
(When you get an error about "Access denied", please execute this as Administrator)
The output of ICACLS should explain why you do no have access to that file/directory.

Couldn't process file resx due to its being in the Internet or Restricted zone or having the mark of the web on the file

I am facing an issue while debugging c# API Coding in Visual studio 2017. Debugging not started and showing a error message like
Couldn't process file resx due to its being in the Internet or Restricted zone or having the mark of the web on the file. Remove the mark of the web if you want to process these files.
Any idea about this error message?
If you downloaded the file from the internet, either separately or inside a .zip file or similar, it may have been "locked" because it is flagged as coming from the internet zone. Many programs will use this as a sign that the content should not be trusted.
The simplest solution is to right-click the file in Windows Explorer, select Properties, and along the bottom of this dialog, you should have an "Unblock" option. Remember to click OK to accept the change.
If you got the file from an archive, it is usually better to unblock the archive first, if the file is flagged as coming from the internet zone, and you unzip it, that flag might propagate to many of the files you just unarchived. If you unblock first, the unarchived files should be fine.
There's also a Powershell command for this, Unblock-File:
> Unblock-File *
Additionally, there are ways to write code that will remove the lock as well.
From the comments by #Defcon1: You can also combine Unblock-File with Get-ChildItem to create a pipeline that unblocks file recursively. Since Unblock-File has no way to find files recursively by itself, you have to use Get-ChildItem to do that part.
> Get-ChildItem -Path '<YOUR-SOLUTION-PATH>' -Recurse | Unblock-File
Open the file explorer. Navigate to project/solution directory
Search for *.resx. --> You will get list of resx files
Right click the resx file, open the properties and check the option 'Unblock'
Repeat #3 for each resx file.
Reload the project.
None of these answers worked for me, I had to do the following:
Start Menu > type 'Internet Options'.
Select Local intranet zone on the Security tab then click the Sites button
Click Advanced button
Enter file://[computer name]
Make sure 'Require server verification...' is unticked
Source: https://superuser.com/q/44503
Find the path from error log and open the file in explorer
2)select the file and right-click -> properties
Then check the 'unblock' option and click on apply
Complementing #lasse-v-karlsen answer.
To unblock all files recursively, run from powershell as administrator inside the folder you want:
gci -recurse | Unblock-File
source link:
How to Unblock Files Downloaded from Internet? - Winhelponline
https://www.winhelponline.com/blog/bulk-unblock-files-downloaded-internet/
Although this is an older question, I spent several hours tracking down a way to handle this error when it applies to multiple files that are located in sub folders throughout the project.
To fix this for all files within a project, Visual Studio -> Tools -> Options -> Trust Settings and add the project path as a trusted path.
If you are using OneDrive, or any similar network drive, you have 2 options:
1) the easy one is to move the folder to a local directory inside your PC (eg:. C:).
2) but if you want to keep using OneDrive I would recommend to add it to the trusted sites on the internet explorer options and that will fix the problem.
I had this issue on resx files in my solution. I'm using Onedrive. However none of the above solutions fixed it.
The problem was the icon I used was in the MyWindow.resx files for the windows.
I removed that then grabbed the icon from the App Local Resources resource folder.
private ResourceManager rm = App_LocalResources.LocalResources.ResourceManager;
..
InitializeComponent();
this.Icon = (Icon)rm.GetObject("IconName");
This happened after an update to VS2019.
None of the above worked.
The "Unblock" option is not present in the explorer properties.
Recreating file, adding folder (and resx file) to Tools->Options->Trust Settings does not work.
The solution was to copy the project locally (from the network drive).
Solution: Edit and save the file!
From VisualStudio go to the View and expand to see it's resx file
Right-click menu select OpenWith... XML (Text) Editor.
Just add a space at the end and save.
If, like me, you have diligently followed all the above solutions and the error is still there, try closing and reopening Visual Studio.
Obvious, I know, but perhaps I'm not the only one who's become fuzzy-brained after staring at a computer screen all day.
None of the above worked for me.
This happened to me after I added a new button to a toolstrip on a winform. When the button uses the default image of System.Drawing.Bitmap (in image property) this error arose for me. After I changed it to a trusted image (one added to my resource file with 'Unlock' option checked) this error resolved itself.
I use OneDrive as well and I fixed the issue by adding Microsoft/Onedrive domains to trusted sites.
Internet options->Security->Trusted Sites->Sites-> and add there:
https://marq-my.sharepoint.com
https://*.onedrive.com
https://*.office365.com
https://*.office.com
After that: Close->Ok->Restart PC
References:
https://www.marquette.edu/its/help/onedrive-for-business/onedrive-for-business-as-a-trusted-site.shtml
https://community.spiceworks.com/topic/2230091-add-onedrive-directory-as-trusted-documents-location
I stumbled upon another possible reason of this error. If you use NTFS symbolic links in your project tree, and probably subst'ed drives, you may get this error even if they point to your local drive. If this is the case, try to avoid the situation when .resx files are reached via symlinks.
None of the suggestions above worked for me so I created a new file with a slightly different name and copied the contents of the offending file into the new file, renamed the offending file and renamed the new file with the offending file's name. Worked like a charm. Problem solved.
If the above solutions fail, like they did for me, backup all resx files and then delete them. Then try running the project again.
Its works perfect by using Powershell command as below (It's very powerful cammand to Unblock all files inside a project folder). Below is the example how to use it:
Powershell cammand:
PS C:\Users\kri> Get-ChildItem -Path 'C:\Users\kri\Downloads\ProjectFolder' -Recurse | Unblock-File
The only solution that worked for me was to copy the solution to a folder outside OneDrive, build and run the solution. Finally, copy the folder back to OneDrive
I also faced this issue when i tried to run a downloaded zipped code from github.
Solution: I simply cloned the code from github instead of downloading and code run successfully without errors.

System.UnauthorizedAccessException while deleting dll (C#)

Please read carefully: I know that dlls can be in use. I also know about NTFS permissions, and read-only flag.
I create a text file with content hello.
I rename it to .dll
I try to use File.Delete(...) method to delete it: I got
System.UnauthorizedAccessException
I try to open it with File.OpenWrite(...) I got the same exception.
I've turned out all virus checking software.
I am admin in my Windows 7 machine.
I am the owner of the .dll extension file
I can it delete without any warning from the UI.
Using VS 2012 C# program (.NET 4.5)
Any ideas?
Thanks
SOLVED: (partly): This was my firewall :-(. Somehow it cached and sandboxed my project output executable, and restricted to delete .dlls (not .txts). Although I turned off it, and I rebuild the .exe many times somehow the named exe sandboxed state remained active. I stress it was a many times rebuild and binary changed .exe. Congratulations to Commodo just 2 erratic behaviour in on issue. 1) Why the sandboxing is still active if antivirus has turned off? 2) Why not reevaluate the named .exe file and asking me about sandboxing if the binary has changed? How this is turned out? I renamed the project output to x.exe :-(
This was more the 45 minutes from my life and worktime...
For delete try
File.SetAttributes(file, FileAttributes.Normal);
File.Delete(file);

C# File.Copy access denied error

I am trying to copy files from nunit test build folder to nunit runtime folder but getting
Access denied error
I have a folder path setup like Testproject/bin/debug/something.config and tried the options below but its not working for me.
Deleted the bin folder and then rebuilt it.
Opened closed VS.
Deleted bin folder created new bin folder manually.
Tried to remove readonly in folder option but it again gets set to
readonly.
I found these worked for others in the thread
Unable to copy file. Access to the path is denied
I am also using SVN as source control.
One thing that you haven't tried is to run Visual Studio as an administrator. Right click on it, and select run as administrator.
Sounds like it was locked and abandoned.
I've used LockHunter to unlock files that Windows so often locks and abandons. Even after rebooting the files can sometimes remain locked by a process that no longer exists.
It will also tell you what applications have locked the file.

File.Copy fails when copying a DLL that is not in use

I've built a winforms app (C#) that will take a list of file paths, and copy those files (from a different VS solution) to a new location (In a folder the user specifies) in the same directory structure they currently exist on local file system.
I use the Path class, Directory class etc and everything works wonderfully...except when it reaches a file path that points to a DLL.
The DLLs I am trying to copy are a part of the other solution, and that solution is not currently open.
I have tried restarting computer to make sure visual studio isn't somehow hooking into that DLL even after the solution is closed.
The DLL in question can be copied by regular manual means (i.e. copy and paste shortcut).
So short of creating a batch file in the program, and running xcopy on that DLL path, I don't know of a way to get this to work.
From what I have found from google searches (which isn't much on this particular situation), File.Copy() should work..
Any help would be wonderful, even if it is a link to a duplicate question I may have over looked.
Thanks!
-The error message is: The process cannot access the file [insert file path] because it is being used by another process (The path is definitely correct also)
-Just downloaded and tried to search for the DLL name with Process Explorer.. I also ran a similar exe from command prompt to no avail. It claims nothing is using it. That's why I am utterly baffled by this. Also, I just checked the permissions and everything looks great (i.e. Full Control, owner effective permissions)
-It does not handle open files. It basically build the correct src and dest paths and does a File.Copy() on those. How would I go about handling open files? I'm sure I could figure out if it was open, but what would I do it it were open?
It is not complaining about the file you're trying to copy, it is complaining about the file that you're trying to overwrite with the copy. Lots of candidates for that, virus scanners always get very excited about new DLLs, for example. Or it is loaded into a process, the typical failure mode for trying to implement your own auto-updater.
You can rename the target file to make your copy succeed.
Are you in vista or win7? If so, Check your 'User Account Control Settings'. Sometimes this can interfere with .NET security options and prevent file operations that would otherwise work.
As well as Process Explorer, I would use Process Monitor also from Microsoft so you can see what is happening at the point of failure and allows you to see if anything else is accessing the dll.
Possible culprits are
the program you are running,
your antivirus package
a virus.
If the path it is complaining about is the destination path, then is is possible that the path is too long?
Also, when using Process Explorer, make sure you have enabled the option to show details for all processes and not just your own.
I just ran into this issue as well. I tried copying a .DLL from an FTP server to a local directory (replacing the existing one) and for the life of me I could not get it to work. Keeps giving me an 'Access Denied code: 5' Error.
I then realized that the .DLL on the FTP server was not marked as hidden while the .DLL I was trying to replace was marked as hidden.
Once I changed the local one to also be visible. I had no more issues.
So my solution is:
Make sure both files are visible.
Hope this helps someone

Categories

Resources