1 using System; 2 using System.IO; 3 4 class Test 5 { 6 7 public static void Main() 8 { 9 string path = Path.GetTempFileName(); 10 FileInfo fi1 = new FileInfo(path); 11 12 //Create a file to write to. 13 using (StreamWriter sw = fi1.CreateText()) 14 { 15 sw.WriteLine("Hello"); 16 sw.WriteLine("And"); 17 sw.WriteLine("Welcome"); 18 } 19 20 //Open the file to read from. 21 using (StreamReader sr = fi1.OpenText()) 22 { 23 string s = ""; 24 while ((s = sr.ReadLine()) != null) 25 { 26 Console.WriteLine(s); 27 } 28 } 29 30 try 31 { 32 string path2 = Path.GetTempFileName(); 33 FileInfo fi2 = new FileInfo(path2); 34 35 //Ensure that the target does not exist. 36 fi2.Delete(); 37 38 //Copy the file. 39 fi1.CopyTo(path2); 40 Console.WriteLine("{0} was copied to {1}.", path, path2); 41 42 //Delete the newly created file. 43 fi2.Delete(); 44 Console.WriteLine("{0} was successfully deleted.", path2); 45 46 } 47 catch (Exception e) 48 { 49 Console.WriteLine("The process failed: {0}", e.ToString()); 50 } 51 } 52 }
在地13行就抛出了:
未能找到路径“C:\Users\Administrator\AppData\Local\Temp\”的一部分。
例子网址http://msdn.microsoft.com/zh-cn/library/akth6b1k(v=vs.100).aspx?appId=Dev10IDEF1&l=ZH-CN&k=k(SYSTEM.IO.FILEINFO)&rd=true
看看 你的电脑上有这个目录啊
C:\Users\Administrator\AppData\Local\Temp\
嗯 的确有这个目录啊 但是有什么问题呢?
在第9行之后加上Console.WriteLine(path);,看看输出的路径是什么?
输出这个C:\Users\Administrator\AppData\Local\Temp\
难道要在这个路径下创建一个文档(.txt)才可以吗?请问下 怎么插入链接呀?
@cc_jony: 问题就在这里,正常情况下返回的路径包含以.tmp为扩展名的文件名
@dudu: 哦 这个路径下含有很多以.tmp为扩展名的文件名,