check the considered file is not locked and privilege to work with it.
[PermissionSet(SecurityAction.LinkDemand, Name = "FullTrust")]
private static void SetFileToHidden(string filePath)
{
var attrs = File.GetAttributes(filePath);
File.SetAttributes(filePath, attrs | FileAttributes.Hidden);
}