分解路径为目录名和文件名的方法

来源:互联网 发布:linux grub2 修复 编辑:程序博客网 时间:2024/05/22 07:43

using System;

class Test

{

static void SplitPath(string path,out string dir,out string name)

{

int i = path.Length;

while (i > 0)

{

char ch = path[i-1];

if (ch == '\\' || ch == '/' || ch == ':')

 break;

i--;

}

dir = path.Substring(0,i);

name = path.Substring(i);

}

static void Main()

{

string dir, name;

SplitPath("c:\\windows\\system\\hello.txt",out dir,out name);

Console.WriteLine("目录名:{0}",dir);

                Console.WriteLine("文件名:{0}",name);

}

}

0 0
原创粉丝点击