简体   繁体   中英

C# Windows Forms - Access to path … Denied

-- SOLVED BY --
adding Thread.Sleep(2000) before deleting the file.
Thanks to Ivan.


Hi guys, it seems that this question has a related questions but I think my problem is different.. and it's very odd.

My problem is, am trying to Delete a file.. an PNG file.. but everytime the FileInfo.Delete() is called, I get this Access to path, denied error. I checked the permission and everything is enabled, Read-only - nope. I even add Everyone user but I still get the same error. I read somewhere that if I have some files (except the executable files, dll, etc) .. I must not put in "Program Files" or anywhere I think is not safe.. So I decided to put it in "My Documents" but it's so odd that I still get the same error?

What's wrong with this?

and about the code, well let's just say something like this

FileInfo fi = new FileInfo("C:\\Users\\Administrator\\Documents\\JWMD Themer\\Start_Icon_SettingsS.png");
fi.Delete();

// or alternatively, I can just use

Path.Copy([srcFile], [destFile]);

尝试在 c: like(@"c:\\windows....") 之前添加“@”

您可以使用Unlocker来验证,这不是打开文件句柄的问题。

Being logged as administrator or running application as administrator doesn't give you rights to delete any file you want in NTFS filesystem. Check if administrator have permission to write on this file - if not you will have change owner of file to admin and gave him permission.

如果在相同情况下可以在Windows资源管理器中删除,我认为解决方案应该是使用Windows Shell API

Open Visual Studio as administrator.

I don't know what more difference does it make except than giving you privileges, but i solved this with this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM