首页 新闻 会员 周边

文件操作基础问题

0
[已关闭问题] 关闭于 2010-09-01 11:42

下面是我学习文件操作的示例程序,请各位指教,问题:

(1)如果

if (File.Exists(fileCopyPath))
                {
                    File.Delete(fileCopyPath);
                }

if (File.Exists(newFileName))
                {
                    File.Delete(newFileName);
                }

删除,则运行一遍后,再运行则会在 make a copy 和 rename the file处报错,因为文件已经存在。但如果删除

if (File.Exists(fileCopyPath))
                {
                    File.Delete(fileCopyPath);
                }

后,在FileStream fs = File.Create(filePath);处却不会报错,而且

File.Create("C:\\HELLO.doc");
File.Create("C:\\HELLO.PDF"); 两条语句在已经存在这两个文件的情况下都不会报错,请问这是什么情况啊?

(2)当把一个存在的文件删除再创建一个同名文件时,File. GetCreationTime方法返回的是哪个时间啊?在本程序中Console.WriteLine(File.GetCreationTime(newFileName)) 打印的一直是较早的那个时间。请高手赐教。

public static void test()
        {
            string filePath = "C:\\My Folder\\MyText.text";
            string fileCopyPath = "C:\\My Folder\\MyText_copy.text";
            string newFileName = "C:\\My Folder\\MyText_newCopy.text";

            try
            {
                //---if the file exists then delete it---
                if (File.Exists(filePath))
                {
                    File.Delete(filePath);
                }
                if (File.Exists(fileCopyPath))
                {
                    File.Delete(fileCopyPath);
                }
                if (File.Exists(newFileName))
                {
                    File.Delete(newFileName);
                }
                //---create the file again---
                FileStream fs = File.Create(filePath);
                fs.Close();

                //---make a copy of the file---
                File.Copy(filePath,fileCopyPath);

                //---rename the file---
                File.Move(filePath,newFileName);

                //---display the creation time---
                File.Create("C:\\HELLO.doc");
                File.Create("C:\\HELLO.PDF");
                Console.WriteLine(File.GetCreationTime(newFileName));

                //---make the file read-only and hidden---
                File.SetAttributes(newFileName,FileAttributes.ReadOnly);
                File.SetAttributes(newFileName,FileAttributes.Hidden);

            }
            catch (System.IO.IOException ex)
            {
             Console.WriteLine(ex.Message);
            }
            catch(System.Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

Green Billow的主页 Green Billow | 初学一级 | 园豆:60
提问于:2010-08-26 15:22
< >
分享
所有回答(2)
0

你能不能先自己看下 File.Create 函数的解释啊!

Launcher | 园豆:45045 (高人七级) | 2010-08-26 15:27
0

同上

寒狐 | 园豆:433 (菜鸟二级) | 2010-08-26 15:47
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册