C# 路径 目录 文件操作办法

 1 using System;
 2 using System.IO;
 3 
 4 [, using System;
 5 using System.Collections.Generic;
 6 using System.Linq;
 7 using System.Text;
 8 using System.IO;
 9 
10 namespace _07文件操作1_Path类
11 {
12     class Program
13     {
14         static void Main(string[] args)
15         {
16             //Path类中方法,基本都是对字符串的操作,与实际文件都没关系。
17             //string path = @"C:\afds\aa\fdsf\fds\fsfs\fds\fdsfds\tesfdsf\a.txt";
18             ////这里只是把字符串"C:\a\tes\a.txt"的后缀改成了"C:\a\tes\a.jpg",只是修改了字符串,与实际存储在磁盘上的文件 没有任何关系。
19             //path = Path.ChangeExtension(path, ".jpg");
20             //Console.WriteLine(path);
21 
22             //string s1 = @"c:\a\";
23             //string s2 = "b.txt";
24             //// string s3 = s1 + s2;
25             ////将两个路径连接起来,可以处理末尾的 \ 问题。
26             //string s3 = Path.Combine(s1, s2);
27             //Console.WriteLine(s3);
28 
29 
30 
31             //string s1 = @"c:\windows\test\abc\a.txt";
32             ////1.请获取路径中的文件名
33             //Console.WriteLine(Path.GetFileName(s1));
34             ////2.请获取路径中的目录,不包含文件名
35             //Console.WriteLine(Path.GetDirectoryName(s1));
36 
37             ////3.获取文件名,不包含扩展名
38             //Console.WriteLine(Path.GetFileNameWithoutExtension(s1));
39 
40             ////4.只获取扩展名
41             //Console.WriteLine(Path.GetExtension(s1));
42 
43 
44 
45 
46             ////获取完整路径================
47             ////string txt = File.ReadAllText("a.txt");
48             ////Console.WriteLine(txt);
49             ////Console.ReadKey();'
50             ////获取相对路径对应的绝对路径。
51             //Console.WriteLine(Path.GetFullPath("../../../../../../../../../../a.txt"));
52 
53 
54             //获取当前用户的临时目录。
55             // Console.WriteLine(Path.GetTempPath());
56 
57             //获取一个随机的文件名,并在临时目录下创建这个文件
58            // Console.WriteLine(Path.GetTempFileName());
59 
60             //获取一个随机的文件名(也可以用作文件夹名。)
61             Console.WriteLine(Path.GetRandomFileName());
62 
63             
64             Console.ReadKey();
65         }
66     }
67 }
件操作1_Path类
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.IO;
 6 
 7 namespace _13File类的基本操作
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             ////1.File类来判断文件是否存在?
14             //if (File.Exists(@"c:\1.txt"))
15             //{
16             //    Console.WriteLine("存在!");
17 
18             //}
19             //else
20             //{
21             //    Console.WriteLine("8存在!");
22             //}
23             //File.Delete(@"c:\1.txt");//如果文件不存在也不报异常!
24             //Console.WriteLine("删除成gong!");
25 
26 
27             File.Copy(@"c:\1.flv", @"d:\3.flv", true);
28 
29             Console.WriteLine("拷贝成功!");
30 
31             //File.Move(@"c:\1.flv", @"d:\1.flv");
32             //Console.WriteLine("剪切成功!");
33             Console.ReadKey();
34         }
35     }
36 }
File类的基本操作
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.IO;
 6 
 7 namespace _15文件流的基本操作
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             #region 通过文件流读取磁盘上的文件
14 
15             ////1。创建文件流
16             //FileStream fsRead = new FileStream("1.txt", FileMode.Open);
17 
18             ////2.创建缓冲区
19             //byte[] byts = new byte[fsRead.Length];
20 
21             ////3.开始读取
22             //int r = fsRead.Read(byts, 0, byts.Length);
23 
24             ////4.关闭文件流、释放资源
25             //fsRead.Close();
26             //fsRead.Dispose();
27 
28             //string msg = System.Text.Encoding.UTF8.GetString(byts);
29             //Console.WriteLine(msg);
30             //Console.WriteLine("ok");
31             //Console.ReadKey();
32 
33             #endregion
34 
35             #region 通过文件流写入文件
36             ////1.创建文件流
37             //FileStream fs = new FileStream(@"c:\mywords.txt", FileMode.OpenOrCreate);
38 
39             //string msg = "我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.";
40             ////2.创建byte[]
41             //byte[] byts = System.Text.Encoding.UTF8.GetBytes(msg);
42 
43             ////3.将byts中的内容写入文件中。
44             //fs.Write(byts, 0, byts.Length);
45 
46             //////4.关闭文件流
47             //fs.Flush();
48             //fs.Close();
49             //fs.Dispose();
50             //Console.WriteLine("写入成功!");
51             //Console.ReadKey();
52             #endregion
53 
54             #region using
55             //using (FileStream fs=new FileStream())
56             //{
57             //    fs.Read();
58                 
59             //}
60 
61             #endregion
62         }
63     }
64 }
文件流
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.IO;
 6 
 7 namespace _05StreamReader与StreamWriter
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             #region StreamWriter演示
14 
15             ////1.创建了一个写文本文件的文件流。
16             //using (StreamWriter sw = new StreamWriter(@"c:\writer.txt", true, Encoding.GetEncoding("gb2312")))
17             //{
18             //    for (int i = 0; i < 100; i++)
19             //    {
20             //        sw.WriteLine(i + "=" + i * 10);
21             //    }
22 
23             //}
24             //Console.WriteLine("ok");
25             //Console.ReadKey();
26 
27 
28 
29             #endregion
30 
31             #region StreamReader演示
32             using (StreamReader sr = new StreamReader(@"c:\writer.txt"))
33             {
34                 
35                 //while (!sr.EndOfStream) //只要没有读取到文件流末尾,就继续读取
36                 //{
37                 //    Console.WriteLine(sr.ReadLine());
38                 //}
39 
40 
41                 //2.
42                 //string result = sr.ReadLine();
43                 //while (result != null)
44                 //{
45                 //    Console.WriteLine(result);
46                 //    result = sr.ReadLine();
47                 //}
48                 string result;
49                 while ((result = sr.ReadLine()) != null)
50                 {
51                     Console.WriteLine(result);
52                 }
53             }
54             Console.WriteLine("ok");
55             Console.ReadKey();
56             #endregion
57         }
58     }
59 }
StreamReader与StreamWriter
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.IO;
 6 
 7 namespace _08Directory类
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             
14             //Directory
15             //1.创建一些个目录
16             //// Directory.CreateDirectory(@"c:\test\1");
17 
18             //for (int i = 0; i < 1000; i++)
19             //{
20             //    Directory.CreateDirectory(@"c:\test\" + i);
21             //}
22             //Console.WriteLine("ok");
23 
24             ////2.获取当前目录下的所有的直接子目录
25             ////string[] dirs = Directory.GetDirectories(@"c:\");
26             //string[] dirs = Directory.GetDirectories(@"c:\","*I*",SearchOption.TopDirectoryOnly);
27             //foreach (string item in dirs)
28             //{
29             //    Console.WriteLine(item);
30             //}
31             ////string[] files = Directory.GetFiles(@"c:\");
32             //string[] files = Directory.GetFiles(@"c:\","*.txt",SearchOption.TopDirectoryOnly);
33             //Console.WriteLine("=========================================");
34             //foreach (string item in files)
35             //{
36             //    Console.WriteLine(item);
37             //}
38 
39 
40             ////3.判断某个目录下是否存在某个文件夹
41             //string path = @"c:\test1";
42             //if (Directory.Exists(path))
43             //{
44             //    Console.WriteLine("存在test1目录");
45 
46             //}
47             //else
48             //{
49             //    Console.WriteLine("不存在test1目录。");
50             //}
51 
52             ////删除c:\test1目录
53             ////如果目录不存在则删除的时候报异常,所以一般删除前都会加一个Directory.Exists()判断目录是否存在。
54             ////Directory.Delete(路径),这个重载,只能删除空目录,如果目录下不为空,则报异常。
55          
56             ////Directory.Delete(path);
57             //Directory.Delete(path, true); //true表示,将当前目录以及里面的所有内容都删除。
58             //Console.WriteLine("删除成功!");
59 
60             //Directory.Move(@"c:\Test.exe", @"c:\a\Test.yzk");
61 
62             ////重命名
63             //Directory.Move(@"c:\a", @"c:\abc");
64             //Console.WriteLine("移动成功!");
65 
66 
67             //把一个文件夹当做一个对象来使用。
68             DirectoryInfo dirInfo = new DirectoryInfo(@"c:\abc");
69 
70             //通过dirInfo对象,就可以获取c:\abc下的所有的文件与文件夹
71             FileInfo[] finfos = dirInfo.GetFiles();
72             DirectoryInfo[] dinfos = dirInfo.GetDirectories();
73             Console.ReadKey();
74         }
75     }
76 }
08Directory类

 

posted @ 2013-07-24 21:10  墓地de小草  阅读(244)  评论(0编辑  收藏  举报