当有多个字符时,如何将 IndexOf 与 SubString 一起使用来选择特定字符?这是我的问题。我想采用路径“C:\Users\Jim\AppData\Local\Temp\”并删除“Temp\”部分。只留下“C:\Users\Jim\AppData\Local\”我已经用下面的代码解决了我的问题,但这假设“Temp”文件夹实际上被称为“Temp”。有没有更好的办法?谢谢
if (Path.GetTempPath() != null) // Is it there?{
tempDir = Path.GetTempPath(); //Make a string out of it.
int iLastPos = tempDir.LastIndexOf(@"\");
if (Directory.Exists(tempDir) && iLastPos > tempDir.IndexOf(@"\"))
{
// Take the position of the last "/" and subtract 4.
// 4 is the lenghth of the word "temp".
tempDir = tempDir.Substring(0, iLastPos - 4);
}}