ASP.NET设计网络硬盘之两重要类

news/2024/7/10 5:35:14 标签: asp.net, 网络, string, path, makefile, c
cle class="baidu_pl">
cle_content" class="article_content clearfix">
content_views" class="htmledit_views"> ck;" class="content" id="BodyLabel">要进行“class="tags" href="/tags/WangLuo.html" title=网络>网络硬盘”功能设计࿰c;首先要熟悉.NET中处理文件和文件夹的操作。File类和Directory类是其中最主要的两个类。了解它们将对后面功能的实现提供很大的便利。

  System.IO.File类和System.IO.FileInfo类

  在设计和实现“class="tags" href="/tags/WangLuo.html" title=网络>网络硬盘”的过程中࿰c;将大量地使用和文件系统操作相关的内容。故本节先对和文件系统相关的两个.NET类进行简要介绍。

  System.IO.File类和System.IO.FileInfo类主要提供有关文件的各种操作࿰c;在使用时需要引用System.IO命名空间。下面通过程序实例来介绍其主要属性和方法。

  (1) 文件打开方法:File.Open

  该方法的声明如下:

  

  public static FileStream Open(class="tags" href="/tags/STRING.html" title=string>string class="tags" href="/tags/PATH.html" title=path>path,FileMode mode)

  下面的代码打开存放在c:/tempuploads目录下名称为newFile.txt文件࿰c;并在该文件中写入hello。

  

  private void OpenFile()

  {

   FileStream.TextFile=File.Open(@"c:/tempuploads/newFile.txt",FileMode.Append);

   byte [] Info = {(byte)'h',(byte)'e',(byte)'l',(byte)'l',(byte)'o'};

   TextFile.Write(Info,0,Info.Length);

   TextFile.Close();

  }

  (2) 文件创建方法:File.Create

  该方法的声明如下:

  

  public static FileStream Create(class="tags" href="/tags/STRING.html" title=string>string class="tags" href="/tags/PATH.html" title=path>path;)

  下面的代码演示如何在c:/tempuploads下创建名为newFile.txt的文件。

  由于File.Create方法默认向所有用户授予对新文件的完全读/写访问权限࿰c;所以文件是用读/写访问权限打开的࿰c;必须关闭后才能由其他应用程序打开。为此࿰c;所以需要使用FileStream类的Close方法将所创建的文件关闭。

  

  private void MakeFile()

  {

   FileStream NewText=File.Create(@"c:/tempuploads/newFile.txt");

   NewText.Close();

  }

  (3) 文件删除方法:File.Delete

  该方法声明如下:

  

  public static void Delete(class="tags" href="/tags/STRING.html" title=string>string class="tags" href="/tags/PATH.html" title=path>path);

  下面的代码演示如何删除c:/tempuploads目录下的newFile.txt文件。

  

  private void DeleteFile()

  {

   File.Delete(@"c:/tempuploads/newFile.txt");

  }

  (4) 文件复制方法:File.Copy

  该方法声明如下:

  

  public static void Copy(class="tags" href="/tags/STRING.html" title=string>string sourceFileName,class="tags" href="/tags/STRING.html" title=string>string destFileName,bool overwrite);

  下面的代码将c:/tempuploads/newFile.txt复制到c:/tempuploads/BackUp.txt。

  由于Cope方法的OverWrite参数设为true࿰c;所以如果BackUp.txt文件已存在的话࿰c;将会被复制过去的文件所覆盖。

  

  private void CopyFile()

  {

   File.Copy(@"c:/tempuploads/newFile.txt",@"c:/tempuploads/BackUp.txt",true);

  }

  (5) 文件移动方法:File.Move

  该方法声明如下:

  

  public static void Move(class="tags" href="/tags/STRING.html" title=string>string sourceFileName,class="tags" href="/tags/STRING.html" title=string>string destFileName);

  下面的代码可以将c:/tempuploads下的BackUp.txt文件移动到c盘根目录下。

  注意:

  只能在同一个逻辑盘下进行文件转移。如果试图将c盘下的文件转移到d盘࿰c;将发生错误。

  

  private void MoveFile()

  {

   File.Move(@"c:/tempuploads/BackUp.txt",@"c:/BackUp.txt");

  }

  (6) 设置文件属性方法:File.SetAttributes

  该方法声明如下:

  

  public static void SetAttributes(class="tags" href="/tags/STRING.html" title=string>string class="tags" href="/tags/PATH.html" title=path>path,FileAttributes fileAttributes);

  下面的代码可以设置文件c:/tempuploads/newFile.txt的属性为只读、隐藏。

  

  private void SetFile()

  {

   File.SetAttributes(@"c:/tempuploads/newFile.txt",

   FileAttributes.ReadOnly FileAttributes.Hidden);

  }

  文件除了常用的只读和隐藏属性外࿰c;还有Archive(文件存档状态)࿰c;System(系统文件)࿰c;Temporary(临时文件)等。关于文件属性的详细情况请参看MSDN中FileAttributes的描述。

  (7) 判断文件是否存在的方法:File.Exist

  该方法声明如下:

  

  public static bool Exists(class="tags" href="/tags/STRING.html" title=string>string class="tags" href="/tags/PATH.html" title=path>path);

  下面的代码判断是否存在c:/tempuploads/newFile.txt文件。若存在࿰c;先复制该文件࿰c;然后其删除࿰c;最后将复制的文件移动;若不存在࿰c;则先创建该文件࿰c;然后打开该文件并进行写入操作࿰c;最后将文件属性设为只读、隐藏。

  

  if(File.Exists(@"c:/tempuploads/newFile.txt")) //判断文件是否存在

  {

   CopyFile(); //复制文件

   DeleteFile(); //删除文件

   MoveFile(); //移动文件

  }

  else

  {

   MakeFile(); //生成文件

   OpenFile(); //打开文件

   SetFile(); //设置文件属性

  }

  此外࿰c;File类对于Text文本提供了更多的支持。

  · AppendText:将文本追加到现有文件

  · CreateText:为写入文本创建或打开新文件

  · OpenText:打开现有文本文件以进行读取

  但上述方法主要对UTF-8的编码文本进行操作࿰c;从而显得不够灵活。在这里推荐读者使用下面的代码对txt文件进行操作。

  · 对txt文件进行“读”操作࿰c;示例代码如下:

  

  StreamReader TxtReader = new StreamReader(@"c:/tempuploads/newFile.txt",System.Text.Encoding.Default);

  class="tags" href="/tags/STRING.html" title=string>string FileContent;

  FileContent = TxtReader.ReadEnd();

  TxtReader.Close();

  · 对txt文件进行“写”操作࿰c;示例代码如下:

  

  StreamWriter = new StreamWrite(@"c:/tempuploads/newFile.txt",System.Text.Encoding.Default);

  class="tags" href="/tags/STRING.html" title=string>string FileContent;

  TxtWriter.Write(FileContent);

  TxtWriter.Close();
System.IO.Directory类和System.DirectoryInfo类

  主要提供关于目录的各种操作࿰c;使用时需要引用System.IO命名空间。下面通过程序实例来介绍其主要属性和方法。

  (1) 目录创建方法:Directory.CreateDirectory

  该方法声明如下:

  

  public static DirectoryInfo CreateDirectory(class="tags" href="/tags/STRING.html" title=string>string class="tags" href="/tags/PATH.html" title=path>path);

  下面的代码演示在c:/tempuploads文件夹下创建名为NewDirectory的目录。

  

  private void MakeDirectory()

  {

   Directory.CreateDirectory(@"c:/tempuploads/NewDirectoty");

  }

  (2) 目录属性设置方法:DirectoryInfo.Atttributes

  下面的代码设置c:/tempuploads/NewDirectory目录为只读、隐藏。与文件属性相同࿰c;目录属性也是使用FileAttributes来进行设置的。

  

  private void SetDirectory()

  {

   DirectoryInfo NewDirInfo = new DirectoryInfo(@"c:/tempuploads/NewDirectoty");

   NewDirInfo.Atttributes = FileAttributes.ReadOnly FileAttributes.Hidden;

  }

  (3) 目录删除方法:Directory.Delete

  该方法声明如下:

  

  public static void Delete(class="tags" href="/tags/STRING.html" title=string>string class="tags" href="/tags/PATH.html" title=path>path,bool recursive);

  下面的代码可以将c:/tempuploads/BackUp目录删除。Delete方法的第二个参数为bool类型࿰c;它可以决定是否删除非空目录。如果该参数值为true࿰c;将删除整个目录࿰c;即使该目录下有文件或子目录;若为false࿰c;则仅当目录为空时才可删除。

  

  private void DeleteDirectory()

  {

   Directory.Delete(@"c:/tempuploads/BackUp",true);

  }

  (4) 目录移动方法:Directory.Move

  该方法声明如下:

  

  public static void Move(class="tags" href="/tags/STRING.html" title=string>string sourceDirName,class="tags" href="/tags/STRING.html" title=string>string destDirName);

  下面的代码将目录c:/tempuploads/NewDirectory移动到c:/tempuploads/BackUp。

  

  private void MoveDirectory()

  {

   File.Move(@"c:/tempuploads/NewDirectory",@"c:/tempuploads/BackUp");

  }

  (5) 获取当前目录下的所有子目录方法:Directory.GetDirectories

  该方法声明如下:

  

  public static class="tags" href="/tags/STRING.html" title=string>string[] GetDirectories(class="tags" href="/tags/STRING.html" title=string>string class="tags" href="/tags/PATH.html" title=path>path;);

  下面的代码读出c:/tempuploads/目录下的所有子目录࿰c;并将其存储到字符串数组中。

  

  private void GetDirectory()

  {

   class="tags" href="/tags/STRING.html" title=string>string [] Directorys;

   Directorys = Directory. GetDirectories (@"c:/tempuploads");

  }

     (6) 获取当前目录下的所有文件方法:Directory.GetFiles

  该方法声明如下:

  

  public static class="tags" href="/tags/STRING.html" title=string>string[] GetFiles(class="tags" href="/tags/STRING.html" title=string>string class="tags" href="/tags/PATH.html" title=path>path;);

  下面的代码读出c:/tempuploads/目录下的所有文件࿰c;并将其存储到字符串数组中。

  

  private void GetFile()

  {

   class="tags" href="/tags/STRING.html" title=string>string [] Files;

   Files = Directory. GetFiles (@"c:/tempuploads",);

  }

  (7) 判断目录是否存在方法:Directory.Exist

  该方法声明如下:

  

  public static bool Exists(

   class="tags" href="/tags/STRING.html" title=string>string class="tags" href="/tags/PATH.html" title=path>path;

  );

  下面的代码判断是否存在c:/tempuploads/NewDirectory目录。若存在࿰c;先获取该目录下的子目录和文件࿰c;然后其移动࿰c;最后将移动后的目录删除。若不存在࿰c;则先创建该目录,然后将目录属性设为只读、隐藏。

  

  if(File.Exists(@"c:/tempuploads/NewDirectory")) //判断目录是否存在

  {

   GetDirectory(); //获取子目录

   GetFile(); //获取文件

   MoveDirectory(); //移动目录

   DeleteDirectory(); //删除目录

  }

  else

  {

   MakeDirectory(); //生成目录

   SetDirectory(); //设置目录属性

  }

  注意:

  路径有3种方式࿰c;当前目录下的相对路径、当前工作盘的相对路径、绝对路径。以C:/Tmp/Book为例(假定当前工作目录为C:/Tmp)。“Book”࿰c;“/Tmp/Book”,“C:/Tmp/Book”都表示C:/Tmp/Book。

  另外࿰c;在C#中 “/”是特殊字符࿰c;要表示它的话需要使用“//”。由于这种写法不方便࿰c;C#语言提供了@对其简化。只要在字符串前加上@即可直接使用“/”。所以上面的路径在C#中应该表示为“Book”࿰c;@“/Tmp/Book”࿰c;@“C:/Tmp/Book”。
cle>

http://www.niftyadmin.cn/n/1850714.html

相关文章

孟岩一个“技术文化人”的片段感悟

http://www.programmer.com.cn/5364/一个“技术文化人”的片段感悟 作者: chenqiuge 分类:架构实践 阅读:15,182 次 添加评论2003 年我加入CSDN,6年之后离开。在2003年之后,我的技术身份就很难界定了。曾经有个朋友称我…

Oracle select * from table 存储过程

前段时间 写Oracle 的存储过程 遇到了很多问题 ,用存储过程 显示一个select * from table 就那么费劲吗?在Google和Baidu上找了好长时间,终于找到了正确答案。 Result Sets from Stored Procedures In Oracle A frequently asked question is: Id like …

Forrester:2011年数据库安全市场概览

美国时间2011年9月19日,forrester发布了最新一期的《2011年数据库安全市场概览》: Enterprise databases continue to experience growing attacks despite enhanced security processes and increasing database security approaches. Security gaps in…

oracle中使用SQL递归语句(转)

场景:常见的领导关系树结构,知道某一节点ID,要查出此节点的所有下级(直接下级和间接下级),此时需要使用SQL递归语句。oracle中的递归语句: start withconnect byprior .例子:…

DCD 和 Space的区别,有验证

在中断向量表中不直接LDR PC,"异常地址".而是使用一个标号,然后再在后面使用DCD定义这个标号,其原因是:LDR 指令只能跳到当前PC 4kB 范围内,而B 指令能跳转到32MB 范围,而现在这样在LDR PC, "xxxx"这条指令不远处用"xxxx"…

扩展中国剩余定理

P4777 【模板】扩展中国剩余定理&#xff08;EXCRT&#xff09; 1 #include <cstdio>2 #include <cstdlib>3 #include <cmath>4 #include <cstring>5 #include <algorithm>6 using namespace std; 7 #define ll long long 8 //#define int128 …

Oracle 的随机数、随机日期和时间、随机字符串

1. 随机数包 SELECTDBMS_RANDOM.VALUE FROMDUAL;2. 在[0..100]范围内取随机数 SELECTTRUNC (DBMS_RANDOM.VALUE (0, 100)) FROMDUAL;3. 大于字符‘A’的10个字符随机字符串 SELECTDBMS_RANDOM.STRING (A, 10) FROMDUAL;4. 单个小写随机字符 SELECTCHR (ROUND(DBMS_RANDOM.VA…

css之background-repeat的几种属性

background-repeat: 指背景图片的重复与否以及重复方式, 有no-repeat, repeat, repeat-x, repeat-y四种属性值.no-repeat: 即无论背景图片的大小, 只显示单个背景图片, 如首页的第一篇文章标题前的”NEW”图标, 代码如上所示;repeat: 指背景图片横向和纵向重复连续显示;repeat-…