C#设置文件夹为属性,编写的时候经常不注意的问题。
来源:互联网 发布:51单片机最小系统制作 编辑:程序博客网 时间:2024/05/10 19:26
using System; 02 using System.IO; 03 using System.Collections; 04 using System.Collections.Generic; 05 Queue<FileSystemInfo> filefolders = new Queue<FileSystemInfo>(new DirectoryInfo(@"C:\cdrom").GetFileSystemInfos()); 06 while (filefolders.Count>0) 07 { 08 FileSystemInfo atom = filefolders.Dequeue(); 09 FileInfo file = atom as FileInfo; 10 if (file == null) 11 { 12 DirectoryInfo directory = atom as DirectoryInfo; 13 foreach (FileSystemInfo fi in directory.GetFileSystemInfos()) 14 filefolders.Enqueue(fi); 15 } 16 else 17 file.Attributes&= ~FileAttributes.ReadOnly; 18 } private void button1_Click(object sender, System.EventArgs e) {//浏览文件夹 if(this.folderBrowserDialog1.ShowDialog()==DialogResult.OK) { this.textBox1.Text=this.folderBrowserDialog1.SelectedPath; } } private void button2_Click(object sender, System.EventArgs e) {//获取文件夹属性 this.checkBox3.Checked=false; this.checkBox4.Checked=false; if(this.textBox1.Text.Length<2) return; //获取文件夹创建时间 this.dateTimePicker1.Text=Directory.GetCreationTime(this.textBox1.Text).ToLongDateString(); //获取文件夹最近被修改时间 this.dateTimePicker2.Text=Directory.GetLastWriteTime(this.textBox1.Text).ToLongDateString(); //获取文件夹最近被访问时间 this.dateTimePicker3.Text=Directory.GetLastAccessTime(this.textBox1.Text).ToLongDateString(); //取得文件夹属性 FileAttributes MyAttributes=File.GetAttributes(this.textBox1.Text); string MyFileType=MyAttributes.ToString(); if(MyFileType.LastIndexOf("Hidden")!=-1) {//判断文件夹隐藏属性 this.checkBox3.Checked=true; } if(MyFileType.LastIndexOf("Archive")!=-1) {//判断文件夹归档属性 this.checkBox4.Checked=true; } } private void button3_Click(object sender, System.EventArgs e) {//设置文件夹属性 if(this.textBox1.Text.Length<2) return; //设置文件夹属性为正常 File.SetAttributes(this.textBox1.Text, FileAttributes.Normal); FileAttributes MyAttributes=File.GetAttributes(this.textBox1.Text); if(this.checkBox3.Checked==true) {//设置文件夹隐藏属性 File.SetAttributes(this.textBox1.Text,MyAttributes|FileAttributes.Hidden); } MyAttributes=File.GetAttributes(this.textBox1.Text); if(this.checkBox4.Checked==true) {//设置文件夹归档属性 File.SetAttributes(this.textBox1.Text,MyAttributes|FileAttributes.Archive); } //设置文件夹创建时间 Directory.SetCreationTime(this.textBox1.Text,this.dateTimePicker1.Value); //设置文件夹最近被修改时间 Directory.SetLastWriteTime(this.textBox1.Text,this.dateTimePicker2.Value); //设置文件夹最近被访问时间 Directory.SetLastAccessTime(this.textBox1.Text,this.dateTimePicker3.Value); MessageBox.Show("设置文件夹属性操作成功!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information); } C# 去除文件和文件夹的只读属性 当我们使用 DirectoryInfo dir = Directory.CreateDirectory(pathName) 创建目录或者创建一个文件后,有时作为临时文件用完以后需要删除掉,使用File.delete()或者Directory.Delete()经常会遇到“访问被拒绝的错误”;这时我们需要设置文件或者文件夹的只读属性,再进行删除。去除文件夹的只读属性: System.IO.DirectoryInfo DirInfo = new DirectoryInfo(“filepath”); DirInfo.Attributes = FileAttributes.Normal & FileAttributes.Directory;去除文件的只读属性: System.IO.File.SetAttributes("filepath", System.IO.FileAttributes.Normal); FileAttributes 枚举提供文件和目录的属性。此枚举有一个 FlagsAttribute 属性,允许其成员值按位组合。命名空间:System.IO程序集:mscorlib(在 mscorlib.dll 中) 语法[SerializableAttribute] [FlagsAttribute] [ComVisibleAttribute(true)] public enum FileAttributes 成员名称 说明 Archive 文件的存档状态。应用程序使用此属性为文件加上备份或移除标记。 Compressed 文件已压缩。 Device 保留供将来使用。 Directory 文件为一个目录。 Encrypted 该文件或目录是加密的。对于文件来说,表示文件中的所有数据都是加密的。对于目录来说,表示新创建的文件和目录在默认情况下是加密的。 Hidden 文件是隐藏的,因此没有包括在普通的目录列表中。 Normal 文件正常,没有设置其他的属性。此属性仅在单独使用时有效。 NotContentIndexed 操作系统的内容索引服务不会创建此文件的索引。 Offline 文件已脱机。文件数据不能立即供使用。 ReadOnly 文件为只读。 ReparsePoint 文件包含一个重新分析点,它是一个与文件或目录关联的用户定义的数据块。 SparseFile 文件为稀疏文件。稀疏文件一般是数据通常为零的大文件。 System 文件为系统文件。文件是操作系统的一部分或由操作系统以独占方式使用。 Temporary 文件是临时文件。文件系统试图将所有数据保留在内存中以便更快地访问,而不是将数据刷新回大容量存储器中。不再需要临时文件时,应用程序会立即将其删除。 FileAttributes的简单应用常用的FileAttributes成员有Hidden,System,Archive,ReadOnly,Directory等等。这些对象可以进行位域运算,通过位或运算给文件附上属性。如:File.setAttribute(filename,FileAttribute.Hidden|FileAttribute.ReadOnly)则,filename文件就拥有Hidden和ReadOnly两种属性。在数值和标志枚举常量之间执行按位“与”操作就可以测试数值中是否已设置标志,这种方法会将数值中与标志不对应的所有位都设置为零,然后测试该操作的结果是否等于该标志枚举常量。仍然是MSDN中的例子: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 does not exist.if (!File.Exists(path)){File.Create(path);}if ((File.GetAttributes(path) & FileAttributes.Hidden) == FileAttributes.Hidden){// Show the file.File.SetAttributes(path, FileAttributes.Archive);Console.WriteLine("The {0} file is no longer hidden.", path);}else{// Hide the file.File.SetAttributes(path, File.GetAttributes(path) | FileAttributes.Hidden);Console.WriteLine("The {0} file is now hidden.", path);}}}ps:FileAttributes.Archive是文档的存档状态,应用程序使用该属性为文件加上备份或删除标记。
=============================
同步调用和异步调用的区别
internal delegate void MyDelegate();
private void DoSomething()
{
try
{
System.Threading.Thread.Sleep(5000);
}
finally
{
}
}
private void btnSync_Click(object sender, EventArgs e)
{
DoSomething();
}
private void btnAnsyc_Click(object sender, EventArgs e)
{
MyDelegate dl = DoSomething;
dl.Invoke();
}
private void btnAnsycBegin_Click(object sender, EventArgs e)
{
MyDelegate dl = DoSomething;
dl.BeginInvoke(null,null);
}
Invoke和同步调用的区别,效果是一样的。立即调用,但是Invoke是线程安全的,系统都会SLEEP。
BeginInvoke是异步调用,程序后台线程在执行,进入队列,系统不会等待,响应较好,但是调用时间可能不确定。
- C#设置文件夹为属性,编写的时候经常不注意的问题。
- 一个不注意导致的莫名其妙问题
- C#编写的时候应该注意哪些问题?
- html编写的过程中,为什么font设置属性的时候,第二个属性不起作用
- result重定向类型为redirectAction时候经常提示Undefined namespace parameter value问题的解决
- c# 设置文件夹属性
- 平时不注意的难点
- 统计数据的时候,经常遇到重复问题,解决方案!
- 在myeclipse拷贝项目时候经常遇到的问题
- java中Integer值比较不注意的问题
- java中Integer值比较不注意的问题
- java中Integer值比较不注意的问题
- Java中Integer值比较不注意的问题
- java中Integer值比较不注意的问题
- android开发 不注意的异常
- Android---不注意的小细节
- 如何修改文件夹和文件被设置为隐藏的属性
- 使用BROWSEINFO 设置浏览文件夹的属性
- Android 启动另一个apk
- Poj 2299 Ultra-QuickSort
- JQuery JSON
- ognl.OgnlException: target is null for setProperty(null, "newsid", [Ljava.lan
- 在每一行的结束处使用CRLF
- C#设置文件夹为属性,编写的时候经常不注意的问题。
- dblink表或视图不存在
- 集成Axis2
- javafx ComboBox 两种转换数据的模式
- Android Studio 下载,安装
- python检索目录下所有文件中包含指定字符串的文件
- 浅谈C++多态性
- [Bluetooth] Android Bluetooth
- 回溯法---->哈密顿环