gpt4 book ai didi

c# - C#中字符串的文件路径

转载 作者:太空宇宙 更新时间:2023-11-03 17:24:52 25 4
gpt4 key购买 nike

我需要从 String 驱动器、目录和扩展名中取出。当我通过驱动器时,我似乎无法获得工作目录和扩展名。

namespace ConsoleApplication1
{
class GetMethods
{
public String GetDrive(String Path)
{
String Drive;
Drive = Path.Substring(0, 2);
Console.WriteLine("Drive: {0}", Drive);
return Drive;
}

public String GetDirectory(String Path)
{
Console.WriteLine("Directorul: ");
var start = Path.IndexOf(":") + 1;
var match2 = Path.Substring(start, Path.IndexOf(".") - start);
return Path;
}

public String GetExtension(String Path)
{
String Extension;
Extension = Path.Substring(0,3);
Console.WriteLine("Extensia: {0}", Extension);
return Extension;
}
}

class Program
{
static void Main(string[] args)
{
String Path;
GetMethods G = new GetMethods();
Console.WriteLine("Introduceti calea: ");
Path =Console.ReadLine();
Console.WriteLine("Calea introdusa este:");
Console.WriteLine(Path);

Console.WriteLine(G.GetDrive(Path));
Console.WriteLine(G.GetDirectory(Path));
Console.WriteLine(G.GetExtension(Path));
}
}
}

最佳答案

使用Path上课以获得你想要的一切:

string path = @"C:\hello\world.txt";
var drive = Path.GetPathRoot(path); // "C:\"
var extension = Path.GetExtension(path); // ".txt"
var directory = Path.GetDirectoryName(path); // "C:\\hello"

关于c# - C#中字符串的文件路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20399980/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com