0 امتیاز
FileStream f = new FileStream("name.cs", FileMode.Create);
StreamWriter stw = new StreamWriter(f);
stw.WriteLine(txt1.Text + txt2.Text);
stw.Close();

من با کد زیر تو یه فایل متنی میخوام که مقدار تکست باکس 1و2 رو بنویسیم. وقتی که فایل در مسیر وجود داشته باشد و خصوصیت Read only اون فعال باشه برنامه ارور میده . چطوری خصوصیت Read only رو با کد غیر فعال کنم تا بتونم تو فایل بنویسم؟

 

بسته شده

1 پاسخ

0 امتیاز
 
بهترین پاسخ

سلام ؛

از متد استاتیک GetAttributes کلاس File می تونی استفاده کنی :

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);
            
            //now the file is not read only !
        }



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

 

البته از کلاس FileInfo و خاصیت IsReadOnly اون هم می تونید استفاده کنید : 

FileInfo fileInfo = new FileInfo("c:\personel.txt");
fileInfo.IsReadOnly = false;

 

 

از کلاس FileInfo استفاده کردم و مشکلم حل شد.Tnx
توسط (1,019 امتیاز) 1 23 74
سوال جدید

2,337 سوال

2,871 پاسخ

3,725 دیدگاه

3,924 کاربر

دسته بندی ها

...