하나 이상의 문자가있는 경우 특정 문자를 선택하기 위해 IndexOf를 SubString과 함께 사용하려면 어떻게해야합니까? 여기에 내 문제가있다. "C : \ Users \ Jim \ AppData \ Local \ Temp \"경로를 가져 와서 "Temp \"부분을 제거하고 싶습니다. 그냥 "C : \ Users \ Jim \ AppData \ Local \"떠나는 아래 코드를 사용하여 내 문제를 해결할 수 있지만 "Temp"폴더 실제로 "Temp"라는 가정합니다. 더 좋은 방법이 있습니까? 이 경로가 이미 될 운명 것을 이해하지 않는 한,하나 이상의 문자가있는 경우 IndexOf를 사용하여 특정 문자를 선택하려면 어떻게해야합니까?
using System;
using System.IO;
class Test
{
static void Main()
{
string path = @"C:\Users\Jim\AppData\Local\Temp\";
DirectoryInfo dir = new DirectoryInfo(path);
DirectoryInfo parent = dir.Parent;
Console.WriteLine(parent.FullName);
}
}
(Directory.GetParent(path)
가 당신에게 임시 디렉토리를 제공합니다 : 감사는
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);
}}
명확히하기 위해 경로의 마지막 서브 디렉토리를 임시 디렉토리인지 여부에 관계없이 제거하고 싶습니까? – Drew
예, TEMP 또는 TMP 중 하나가 될 것입니다 – JimDel