Path相关的方法
2015-10-13 15:43:08 访问(1375) 赞(0) 踩(0)
StringBuilder sb = new StringBuilder();
sb.AppendLine("Path.AltDirectorySeparatorChar = " + Path.AltDirectorySeparatorChar.ToString());
sb.AppendLine("Path.DirectorySeparatorChar = " + Path.DirectorySeparatorChar.ToString());
sb.AppendLine("Path.PathSeparator = " + Path.PathSeparator.ToString());
sb.AppendLine("Path.VolumeSeparatorChar = " + Path.VolumeSeparatorChar.ToString());
sb.AppendLine("Path.InvalidPathChars(个数) = " + Path.InvalidPathChars.Length.ToString());
sb.AppendLine("Path.GetInvalidFileNameChars(个数) = " + Path.GetInvalidFileNameChars().Length.ToString());
sb.AppendLine("Path.GetInvalidPathChars(个数) = " + Path.GetInvalidPathChars().Length.ToString());
//int idx = 0;
//foreach (char c in Path.InvalidPathChars)
//{
// ++idx;
// sb.AppendLine(idx.ToString() + "、" + ((int)c).ToString() + " " + c.ToString());
//}
string newFile = null;
string file = @"C:\tmp\2015\10\cur.bmp";
sb.AppendLine();
sb.AppendLine("相关方法:");
sb.AppendLine("原文件名:" + file);
sb.AppendLine("Path.GetDirectoryName(file) = " + Path.GetDirectoryName(file));
sb.AppendLine("Path.GetExtension(file) = " + Path.GetExtension(file));
sb.AppendLine("Path.GetExtension(file) is null == " + (Path.GetExtension(file) == null).ToString());
sb.AppendLine("Path.GetExtension(file) is empty == " + (Path.GetExtension(file) == string.Empty).ToString());
sb.AppendLine("Path.GetFileName(file) = " + Path.GetFileName(file));
sb.AppendLine("Path.GetFileNameWithoutExtension(file) = " + Path.GetFileNameWithoutExtension(file));
sb.AppendLine("Path.GetFullPath(file) = " + Path.GetFullPath(file));
sb.AppendLine("Path.GetPathRoot(file) = " + Path.GetPathRoot(file));
sb.AppendLine("Path.GetRandomFileName() = " + Path.GetRandomFileName());
sb.AppendLine("Path.GetTempPath() = " + Path.GetTempPath());
sb.AppendLine("Path.HasExtension(file) = " + Path.HasExtension(file));
sb.AppendLine("Path.IsPathRooted(file) = " + Path.IsPathRooted(file));
sb.AppendLine("Path.IsPathRooted(file) = " + Path.IsPathRooted(file));
newFile = Path.ChangeExtension(file,".jpg");
sb.AppendLine("Path.ChangeExtension(file,\".jpg\") = " + newFile);
sb.AppendLine();
file = @"C:\tmp\2015\10\newname";
sb.AppendLine("更改参宿后的方法:");
sb.AppendLine("原文件名:" + file);
sb.AppendLine("Path.GetDirectoryName(file) = " + Path.GetDirectoryName(file));
sb.AppendLine("Path.GetExtension(file) = " + Path.GetExtension(file));
sb.AppendLine("Path.GetExtension(file) is null == " + (Path.GetExtension(file) == null).ToString());
sb.AppendLine("Path.GetExtension(file) is empty == " + (Path.GetExtension(file) == string.Empty).ToString());
sb.AppendLine("Path.GetFileName(file) = " + Path.GetFileName(file));
sb.AppendLine("Path.GetFileNameWithoutExtension(file) = " + Path.GetFileNameWithoutExtension(file));
sb.AppendLine("Path.GetFullPath(file) = " + Path.GetFullPath(file));
sb.AppendLine("Path.GetPathRoot(file) = " + Path.GetPathRoot(file));
sb.AppendLine("Path.GetRandomFileName() = " + Path.GetRandomFileName());
sb.AppendLine("Path.GetTempPath() = " + Path.GetTempPath());
sb.AppendLine("Path.HasExtension(file) = " + Path.HasExtension(file));
sb.AppendLine("Path.IsPathRooted(file) = " + Path.IsPathRooted(file));
sb.AppendLine("Path.IsPathRooted(file) = " + Path.IsPathRooted(file));
newFile = Path.ChangeExtension(file, ".jpg");
sb.AppendLine("Path.ChangeExtension(file,\".jpg\") = " + newFile);
sb.AppendLine();
file = @"C:\tmp/2015/10\ca.txt";
sb.AppendLine("更改参宿后的方法:");
sb.AppendLine("原文件名:" + file);
sb.AppendLine("Path.GetDirectoryName(file) = " + Path.GetDirectoryName(file));
sb.AppendLine("Path.GetExtension(file) = " + Path.GetExtension(file));
sb.AppendLine("Path.GetExtension(file) is null == " + (Path.GetExtension(file) == null).ToString());
sb.AppendLine("Path.GetExtension(file) is empty == " + (Path.GetExtension(file) == string.Empty).ToString());
sb.AppendLine("Path.GetFileName(file) = " + Path.GetFileName(file));
sb.AppendLine("Path.GetFileNameWithoutExtension(file) = " + Path.GetFileNameWithoutExtension(file));
sb.AppendLine("Path.GetFullPath(file) = " + Path.GetFullPath(file));
sb.AppendLine("Path.GetPathRoot(file) = " + Path.GetPathRoot(file));
sb.AppendLine("Path.GetRandomFileName() = " + Path.GetRandomFileName());
sb.AppendLine("Path.GetTempPath() = " + Path.GetTempPath());
sb.AppendLine("Path.HasExtension(file) = " + Path.HasExtension(file));
sb.AppendLine("Path.IsPathRooted(file) = " + Path.IsPathRooted(file));
sb.AppendLine("Path.IsPathRooted(file) = " + Path.IsPathRooted(file));
sb.AppendLine("Path.GetInvalidFileNameChars(个数) = " + Path.GetInvalidFileNameChars().Length.ToString());
sb.AppendLine("Path.GetInvalidPathChars(个数) = " + Path.GetInvalidPathChars().Length.ToString());
newFile = Path.ChangeExtension(file, ".jpg");
sb.AppendLine("Path.ChangeExtension(file,\".jpg\") = " + newFile);
sb.AppendLine();
TextBox1.Text = sb.ToString();
结果输出:
Path.AltDirectorySeparatorChar = /
Path.DirectorySeparatorChar = \
Path.PathSeparator = ;
Path.VolumeSeparatorChar = :
Path.InvalidPathChars(个数) = 36
Path.GetInvalidFileNameChars(个数) = 41
Path.GetInvalidPathChars(个数) = 36
相关方法:
原文件名:C:\tmp\2015\10\cur.bmp
Path.GetDirectoryName(file) = C:\tmp\2015\10
Path.GetExtension(file) = .bmp
Path.GetExtension(file) is null == False
Path.GetExtension(file) is empty == False
Path.GetFileName(file) = cur.bmp
Path.GetFileNameWithoutExtension(file) = cur
Path.GetFullPath(file) = C:\tmp\2015\10\cur.bmp
Path.GetPathRoot(file) = C:\
Path.GetRandomFileName() = ftz1idp1.wjv
Path.GetTempPath() = C:\Users\SX\AppData\Local\Temp\
Path.HasExtension(file) = True
Path.IsPathRooted(file) = True
Path.IsPathRooted(file) = True
Path.ChangeExtension(file,".jpg") = C:\tmp\2015\10\cur.jpg
更改参宿后的方法:
原文件名:C:\tmp\2015\10\newname
Path.GetDirectoryName(file) = C:\tmp\2015\10
Path.GetExtension(file) =
Path.GetExtension(file) is null == False
Path.GetExtension(file) is empty == True
Path.GetFileName(file) = newname
Path.GetFileNameWithoutExtension(file) = newname
Path.GetFullPath(file) = C:\tmp\2015\10\newname
Path.GetPathRoot(file) = C:\
Path.GetRandomFileName() = q33ohmtm.qnl
Path.GetTempPath() = C:\Users\SX\AppData\Local\Temp\
Path.HasExtension(file) = False
Path.IsPathRooted(file) = True
Path.IsPathRooted(file) = True
Path.ChangeExtension(file,".jpg") = C:\tmp\2015\10\newname.jpg
更改参宿后的方法:
原文件名:C:\tmp/2015/10\ca.txt
Path.GetDirectoryName(file) = C:\tmp\2015\10
Path.GetExtension(file) = .txt
Path.GetExtension(file) is null == False
Path.GetExtension(file) is empty == False
Path.GetFileName(file) = ca.txt
Path.GetFileNameWithoutExtension(file) = ca
Path.GetFullPath(file) = C:\tmp\2015\10\ca.txt
Path.GetPathRoot(file) = C:\
Path.GetRandomFileName() = 4jspduag.0fr
Path.GetTempPath() = C:\Users\SX\AppData\Local\Temp\
Path.HasExtension(file) = True
Path.IsPathRooted(file) = True
Path.IsPathRooted(file) = True
Path.GetInvalidFileNameChars(个数) = 41
Path.GetInvalidPathChars(个数) = 36
Path.ChangeExtension(file,".jpg") = C:\tmp/2015/10\ca.jpg
标签:
Path相关的方法 


上一条:
下一条:
相关评论
发表评论