扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:builder.com.cn 2007年4月26日
关键字:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace PathClassCSharp {
class Program {
static void Main(string[] args){
string testPath;
string p1;
string p2;
char[] fnChars;
inti;
testPath = "c:\\techrepublic.txt";
p1 = "c:\\";
p2 = "test.exe";
Console.WriteLine("Directory name: " + Path.GetDirectoryName(testPath));
if (Path.HasExtension(testPath)) {
Console.WriteLine("File extension: " + Path.GetExtension(testPath));
}
Console.WriteLine("Filename: " + Path.GetFileName(testPath));
Console.WriteLine("Filename w/o ext: " + Path.GetFileNameWithoutExtension(testPath));
Console.WriteLine("Full path: " + Path.GetFullPath(testPath));
fnChars = Path.GetInvalidFileNameChars();
if (Path.IsPathRooted(testPath)) {
Console.WriteLine("Root: " + Path.GetPathRoot(testPath));
}
Console.WriteLine("Random file name: " + Path.GetRandomFileName());
Console.WriteLine("Temp file name: " + Path.GetTempFileName());
if (File.Exists(testPath)) {
Console.WriteLine("File does exist.");
}
if (File.Exists(Path.Combine(p1, p2))) {
Console.WriteLine("The file " + Path.Combine(p1, p2) + " exists.");
} else {
Console.WriteLine("The file " + Path.Combine(p1, p2) + " does not exist.");
} } } }
濡傛灉鎮ㄩ潪甯歌揩鍒囩殑鎯充簡瑙T棰嗗煙鏈€鏂颁骇鍝佷笌鎶€鏈俊鎭紝閭d箞璁㈤槄鑷抽《缃戞妧鏈偖浠跺皢鏄偍鐨勬渶浣抽€斿緞涔嬩竴銆�