加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

C#删除文件

(2012-08-10 17:20:22)
标签:

只读文件

文件属性

目标文件

txt文件

删除

it

分类: C#

1:protected void Button1_Click(object sender, EventArgs e)
    {
        string pSavedPath1 = Server.MapPath("../../images/ProductImages/6114413/绿.jpg");


        if (File.Exists(pSavedPath1))
        {

            FileInfo fi = new FileInfo(pSavedPath1);

            if (fi.Attributes.ToString().IndexOf("ReadOnly") != -1)

                fi.Attributes = FileAttributes.Normal;

            File.Delete(pSavedPath1);

        }


    }

 

2:

删除只读文件

获取文件的版本信息:

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);

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有