Skip to content

Instantly share code, notes, and snippets.

@Fhernd
Created July 6, 2015 01:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Fhernd/2db05f24a695c3e58573 to your computer and use it in GitHub Desktop.
Save Fhernd/2db05f24a695c3e58573 to your computer and use it in GitHub Desktop.
Uso correcto de aritmética bitwise en la consulta de propiedades de un archivo.
// OrtizOL - xCSw
using System;
using System.IO;
namespace Receta.CSharp.R0502
{
public class UsoCorrectoBitwise
{
public static void Main()
{
// El archivo `WindowsShell.Manifest` es de sólo lectura,
// oculto, y archivo:
FileInfo archivo = new FileInfo("WindowsShell.Manifest");
// Muestra los atributos actuales del archivo:
Console.WriteLine("\nPropiedades de WindowsShell.Manifest: " +
archivo.Attributes.ToString());
// Intenta comprobar si el archivo WindowsShell.Manifest es
// de sólo lectura con uso incorrecto:
if (archivo.Attributes == FileAttributes.ReadOnly)
{
Console.WriteLine("El archivo es de sólo lectura. (Prueba errónea).");
}
// Esta es la forma correcta de comprobar si el archivo WindowsShell.Manifest
// es de sólo lectura. Se utiliza el operador bitwise AND (&):
if ((archivo.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
{
Console.WriteLine("\nEl archivo es de sólo lectura. (Prueba CORRECTA.)");
}
Console.WriteLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment