C#:获得文件版本信息及只读文件的删除

来源:互联网 发布:vb中变量名的命名规则 编辑:程序博客网 时间:2024/06/10 01:16
获取文件的版本信息:
FileVersionInfo myFileVersionInfo1 = FileVersionInfo.GetVersionInfo("D://TEST.DLL");
textBox1.Text="版本号: " + myFileVersionInfo1.FileVersion;
更改文件属性,删除只读文件:
下例欲将E:/test.txt文件拷贝至D:/tmp/test.txt,但D:/tmp/test.txt已经存在。
//File.Copy(sourceFile,destinationFile,true); 用来拷贝文件
//当destinationFile已经存在时,无法将文件file1拷贝到目标文件,
//因此先删除destination文件,File.Delete()方法不能删除只读文件,
//因此,如果文件属性为只读(Attributes属性中会包含有"ReadOnly"),
//先把文件属性重置为Normal,然后再删除:
string file1="E://test.txt";
string destinationFile="d://tmp//test.txt";
if(File.Exists(destinationFile))
{
  FileInfo fi=new FileInfo(destinationFile);
  if(fi.Attributes.ToString().IndexOf("ReadOnly")!=-1)
  fi.Attributes=FileAttributes.Normal;
  File.Delete(destinationFile);
}
File.Copy(file1,destinationFile,true);
如何在asp.net中操作文件
在asp.net操作文件的所有concept都在system.io namespace中,这个namespace包含读写操作文件所必需的类。本文将详细介绍关于文件读,写,删除等asp.net中的文件操作。
创建文件:并写入一些内容。
正如开始我们提到的,我们需要在我们的asp.net的页面的前面添加文件操作的namespace“system.io”
第一步:
如下所示添加namespace
<%@ Import Namespace="System.IO" %>
下一步制作文本文件
writefile.aspx
<%@ Import Namespace="System.IO" %>
<%
Response.write("Writing the content into Text File in ASP.NET <BR>")
" 声明streamwriter对象
Dim strwriterobj As StreamWriter
" 创建文本文件并将其赋给上面声明的streamwriter对象
strwriterobj= File.CreateText("c:/aspnet.txt" )
"在刚才创建的文本文件里写一些东东
strwriterobj.WriteLine( "Welcome to user chenyang"s ASP.NET Program" )
strwriterobj.Close
Response.write("创建文本文件并填充内容")
%>
现在我们完成了第一部分,接下来,我们来完成第二部分
从文件中读取数据
1.读取文件使用StreamReader类
2.当使用readline时,文件的末尾用空串表示("")
让我们从我们刚刚制作的文本文件中读取数据
readfile.aspx
<%@ Import Namespace="System.IO" %>
<%
Response.write("Reading the content from the text file ASPNET.TXT <br>")
" 声明streamreader对象
Dim streamreaderobj As StreamReader
" 声明filecont变量储存文件中读取的数据
Dim filecont As String
" 打开文本文件并赋于streamreaderobj对象
streamreaderobj = File.OpenText( "c:/aspnet.txt" )
" 读取文件数据直到空值为止
Do
filecont = streamreaderobj.ReadLine()
Response.Write( filecont & "<br>" )
Loop Until filecont = ""
" 操作完成关闭streamreaderobj对象
streamreaderobj.Close
Response.write("<br> 读取aspnet.txt文件结束")
%>

三.删除文件
删除文件的操作可以说在asp.net中是最直接的。
下面让我们看看文件的删除操作
Filedelete.aspx
<%@ Import Namespace="System.IO" %>
<%
File.Delete("c:/aspnet.txt" )
Response.write("文件删除成功!!!" )
%>
ASP.NET直接下载一个文件,而不是在IE中打开它 
  有的时候我们不想让用户直接在IE中打开已知类型的文件,比如Word,而希望能直接下载,这时候可用下面代码来替换Response.Redirect
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment;FileName="+YourFileName);
Response.BinaryWrite((byte[])YourFileData.Rows[0]["AttachmentContent"]);
Response.End();
<a href="download.aspx/hello.chm?fileid=12345">hello.chm</a>
这样客户端下载的时候默认是 hello.chm 这个名字的。
C#计算一个文件夹的大小
private void Form1_Load(object sender, System.EventArgs e)
{
string A = FolderSize(@"C://").ToString();
this.label1.Text = A;
}
public static long FolderFileSize(string path)
{
long size = 0;
try
{
FileInfo [] files = (new DirectoryInfo(path)).GetFiles();
foreach(FileInfo file in files)
{
size += file.Length;
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
return size;
}
public static long FolderSize(string path)
{long Fsize = 0;
try
{
Fsize = FolderFileSize(path);
DirectoryInfo [] folders = (new DirectoryInfo(path)).GetDirectories();
foreach(DirectoryInfo folder in folders)
{
Fsize += FolderSize(folder.FullName);
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
return Fsize;
}  
原创粉丝点击