使用Path对象判断路径的完整性和正确性

using System;
using System.IO; class Test
{ public static void Main()
{
string path1 = @"c:\temp\MyTest.txt";
string path2 = @"c:\temp\MyTest";
string path3 = @"temp"; if (Path.HasExtension(path1))
{
Console.WriteLine("{0} has an extension.", path1);
} if (!Path.HasExtension(path2))
{
Console.WriteLine("{0} has no extension.", path2);
} if (!Path.IsPathRooted(path3))
{
Console.WriteLine("The string {0} contains no root information.", path3);
} Console.WriteLine("The full path of {0} is {1}.", path3, Path.GetFullPath(path3));
Console.WriteLine("{0} is the location for temporary files.", Path.GetTempPath());
Console.WriteLine("{0} is a file available for use.", Path.GetTempFileName()); /* This code produces output similar to the following:
* c:\temp\MyTest.txt has an extension.
* c:\temp\MyTest has no extension.
* The string temp contains no root information.
* The full path of temp is D:\Documents and Settings\cliffc\My Documents\Visual Studio 2005\Projects\ConsoleApplication2\ConsoleApplication2\bin\Debug\temp.
* D:\Documents and Settings\cliffc\Local Settings\Temp\8\ is the location for temporary files.
* D:\Documents and Settings\cliffc\Local Settings\Temp\8\tmp3D.tmp is a file available for use.
*/
}
}

使用FileInfo创建文件

using System;
using System.IO; class Test
{ public static void Main()
{
//通过Path对象获取缓存路径
string path = Path.GetTempFileName();
FileInfo fi1 = new FileInfo(path); //创建文件,并且通过返回的"文件流写入对象"写入数据
using (StreamWriter sw = fi1.CreateText())
{
sw.WriteLine("Hello");
sw.WriteLine("And");
sw.WriteLine("Welcome");
} //打开文件,并且通过返回的"文件流读取对象"读取数据
using (StreamReader sr = fi1.OpenText())
{
string s = "";
while ((s = sr.ReadLine()) != null)
{
Console.WriteLine(s);
}
} try
{
string path2 = Path.GetTempFileName();
FileInfo fi2 = new FileInfo(path2); //删除文件
fi2.Delete(); //将f1文件拷贝到f2文件
fi1.CopyTo(path2);
Console.WriteLine("{0} was copied to {1}.", path, path2); //删除文件
fi2.Delete();
Console.WriteLine("{0} was successfully deleted.", path2); }
catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
}
}
}

  

最新文章

  1. Activiti5.10简易教程一
  2. C#知识点整理
  3. 【Java EE 学习 28 上】【oracle学习第二天】【子查询】【集合运算】【几种数据库对象】
  4. java树形目录展示
  5. Android 软引用
  6. 同样的MVC,不同的实现方法(Spring MVC .Net MVC)
  7. windows 10磁盘占用100%解决方案
  8. php建立多层目录的函数
  9. BZOJ3548 : [ONTAK2010]Party
  10. 设置php下载文件的超时时间
  11. OOD沉思录 --- 类和对象的关系 --- 包含关系4
  12. iframe 父子窗口相互之间调用语法
  13. Java调用.Net WebService参数为空解决办法 (远程)调试webservice方法 转
  14. 【大盛】HTC one/M7 ROM 最新本地化OrDroid8.2.6 高级、快速设置 永久root 更多自定义 稳定 流畅
  15. 在react中引入下拉刷新和上拉加载
  16. 安装Navicat for MySQL
  17. 【Android Studio安装部署系列】三十六、从Android Studio3.1.4升级到Android studio3.2【以及创建android p模拟器(未成功)】
  18. spark on yarn 内存分配
  19. C# 内存理论与实践
  20. python笔记1-基础概念、python安装使用配置

热门文章

  1. css(非表格变成表格用)
  2. 做.net的早晚会用到,并且网上还没有这方面的正确资料或几乎很少
  3. Spring Boot 系列教程5-热部署-devtools模块
  4. cddiv/数组维护
  5. TForm类有关属性简介
  6. HDU2216:Game III(BFS)
  7. HDU 5323 DFS
  8. UVALive 2035 The Monocycle(BFS状态处理+优先队列)
  9. cocos2d-x3.x Vector
  10. Win7下安装配置Java