La séance de questions et réponses d’aujourd’hui nous est offerte par SuperUser, une sous-division de Stack Exchange, un groupe de sites Web de questions-réponses dirigé par la communauté.
Capture d'écran du Bloc-notes avec la permission de Dan (SuperUser).
La question
Le lecteur SuperUser boboes veut savoir s'il existe une méthode intégrée à Windows 7 et aux éditions plus récentes pour verrouiller un fichier et l'empêcher d'être écrasé ou supprimé:
Is there a way to lock a file with in-house means so that it cannot be deleted or overwritten? I need to temporarily lock files while testing copy scripts in order to check the error handling in those scripts. Until Windows XP, I would load a file in debug.exe to lock it. Is there a way to do this in Windows 7 and newer editions?
I know that there are programs for doing this. My question is if there is a built in mechanism in Windows 7 and newer editions. Sometimes I have to check a script on a PC and do not want to install new programs for that.
There are also some good suggestions here: How to purposefully exclusively lock a file? Unfortunately, they require 3rd party tools or changing the file to a locked state.
Existe-t-il une méthode intégrée pour le faire dans Windows 7 et les éditions plus récentes?
La réponse
Les contributeurs SuperUser, Dan et Breakthrough, ont la solution pour nous. Tout d'abord, Dan:
I think PowerShell is probably the neatest way to accomplish this. Something like the following:
Suivi de la réponse de Breakthrough:
For the curious, here is the MSDN link to the File::Open method detailing the parameters of the call (specifically, the last argument ‘None’, declines sharing the file with any other threads/processes). Likewise, provisions exist to lock the file for read/write only, or a combination thereof.
Avez-vous quelque chose à ajouter à l'explication? Sound off dans les commentaires. Voulez-vous lire plus de réponses d'autres utilisateurs de Stack Exchange doués en technologie? Découvrez le fil de discussion complet ici.