Skip to content

Instantly share code, notes, and snippets.

@dejanstojanovic
Last active July 18, 2019 03:17
Show Gist options
  • Save dejanstojanovic/284e0355d9944572d30d430d9c64e059 to your computer and use it in GitHub Desktop.
Save dejanstojanovic/284e0355d9944572d30d430d9c64e059 to your computer and use it in GitHub Desktop.
using System.IO;
namespace IOExtensions
{
public static class DirectoryExtensions
{
public static bool IsHidden(this DirectoryInfo dir)
{
return (dir.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden;
}
public static bool IsReadonly(this DirectoryInfo dir)
{
return (dir.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly;
}
public static DirectoryInfo SetHidden(this DirectoryInfo dir, bool hidden)
{
if (hidden)
{
dir.Attributes |= FileAttributes.Hidden;
}
else
{
dir.Attributes &= ~FileAttributes.Hidden;
}
return dir;
}
public static DirectoryInfo SetReadonly(this DirectoryInfo dir, bool @readonly)
{
if (@readonly)
{
dir.Attributes |= FileAttributes.ReadOnly;
}
else
{
dir.Attributes &= ~FileAttributes.ReadOnly;
}
return dir;
}
}
}
using System.IO;
namespace IOExtensions
{
public static class FileExtensions
{
public static bool IsHidden(this FileInfo file)
{
return (file.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden;
}
public static bool IsReadonly(this FileInfo file)
{
return (file.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly;
}
public static FileInfo SetHidden(this FileInfo file, bool hidden)
{
if (hidden)
{
file.Attributes |= FileAttributes.Hidden;
}
else
{
file.Attributes &= ~FileAttributes.Hidden;
}
return file;
}
public static FileInfo SetReadonly(this FileInfo file, bool @readonly)
{
if (@readonly)
{
file.Attributes |= FileAttributes.ReadOnly;
}
else
{
file.Attributes &= ~FileAttributes.ReadOnly;
}
return file;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment