SORU
13 EYLÜL 2011, Salı


Nasıl tek bir Öznitelik (örneğin, salt Okunur bir Dosyayı kaldırmak için?

Diyelim ki, bir dosya aşağıdaki özelliklere sahiptir: salt Okunur, Gizli, Arşiv, Sistem. Nasıl sadece bir özellik kaldırabilir miyim?(örneğin salt Okunur) Eğer kullanırsam:

Io.File.SetAttributes("File.txt",IO.FileAttributes.Normal)

tüm öznitelikleri kaldırır.

CEVAP
13 EYLÜL 2011, Salı


MSDN: Bu gibi herhangi bir öznitelik kaldırabilirsiniz

(ama @sll cevabı için sadece salt Okunur özniteliği) için daha hayırlıdır

using System;
using System.IO;
using System.Text;
class Test 
{
    public static void Main() 
    {
        string path = @"c:\temp\MyTest.txt";

        // Create the file if it exists.
        if (!File.Exists(path)) 
        {
            File.Create(path);
        }

        FileAttributes attributes = File.GetAttributes(path);

        if ((attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
        {
            // Make the file RW
            attributes = RemoveAttribute(attributes, FileAttributes.ReadOnly);
            File.SetAttributes(path, attributes);
            Console.WriteLine("The {0} file is no longer RO.", path);
        } 
        else 
        {
            // Make the file RO
            File.SetAttributes(path, File.GetAttributes(path) | FileAttributes.Hidden);
            Console.WriteLine("The {0} file is now RO.", path);
        }
    }

    private static FileAttributes RemoveAttribute(FileAttributes attributes, FileAttributes attributesToRemove)
    {
        return attributes & ~attributesToRemove;
    }
}

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Canal TekZoom

    Canal TekZoo

    1 NİSAN 2012
  • Dion Coulls

    Dion Coulls

    16 AĞUSTOS 2006
  • SolidWorksTutoriels

    SolidWorksTu

    14 Kasım 2013