탐색기에서 폴더 열기 및 파일 선택 찾을 수

탐색기에서 파일을 선택한 상태로 폴더를 열려고합니다.

다음 코드는 파일을 찾을 수 없음 예외를 생성합니다.

System.Diagnostics.Process.Start(
    "explorer.exe /select,"
    + listView1.SelectedItems[0].SubItems[1].Text + "\\"
    + listView1.SelectedItems[0].Text);

C #에서이 명령을 실행하려면 어떻게해야합니까?



답변

이 방법을 사용하십시오 :

Process.Start(String, String)

첫 번째 인수는 응용 프로그램 (explorer.exe)이고 두 번째 인수는 실행하는 응용 프로그램의 인수입니다.

예를 들면 다음과 같습니다.

CMD에서 :

explorer.exe -p

C #에서 :

Process.Start("explorer.exe", "-p")

답변

// suppose that we have a test.txt at E:\
string filePath = @"E:\test.txt";
if (!File.Exists(filePath))
{
    return;
}

// combine the arguments together
// it doesn't matter if there is a space after ','
string argument = "/select, \"" + filePath +"\"";

System.Diagnostics.Process.Start("explorer.exe", argument);

답변

경로에 쉼표가 포함되어 있으면 Process.Start (ProcessStartInfo)를 사용할 때 경로를 따옴표로 묶어야합니다.

그러나 Process.Start (string, string)를 사용할 때는 작동하지 않습니다. Process.Start (string, string)은 실제로 인수 내부의 따옴표를 제거하는 것처럼 보입니다.

다음은 저에게 적합한 간단한 예입니다.

string p = @"C:\tmp\this path contains spaces, and,commas\target.txt";
string args = string.Format("/e, /select, \"{0}\"", p);

ProcessStartInfo info = new ProcessStartInfo();
info.FileName = "explorer";
info.Arguments = args;
Process.Start(info);

답변

파일 이름에 공백이 포함되어 있으면 내 2 센트 만 사용할 수 있습니다 (예 : “c : \ My File Contains Spaces.txt”). 파일 이름을 따옴표로 묶어야합니다. 그렇지 않으면 탐색기에서 othe 단어가 다른 인수라고 가정합니다.

string argument = "/select, \"" + filePath +"\"";

답변

사무엘 양의 대답이 저를 넘어 뜨 렸습니다. 여기에 3 센트의 가치가 있습니다.

Adrian Hum이 옳습니다. 파일 이름을 따옴표로 묶어야합니다. zourtney가 지적한 것처럼 공백을 처리 할 수 ​​없기 때문에 파일 이름의 쉼표 (및 다른 문자)를 별도의 인수로 인식하기 때문입니다. Adrian Hum이 제안한 것처럼 보일 것입니다.

string argument = "/select, \"" + filePath +"\"";

답변

인수 와 함께 Process.Starton explorer.exe을 사용하면 /select길이가 120 자 미만인 경로에 대해서만 작동합니다.

모든 경우에 작동하려면 기본 Windows 방법을 사용해야했습니다.

[DllImport("shell32.dll", SetLastError = true)]
public static extern int SHOpenFolderAndSelectItems(IntPtr pidlFolder, uint cidl, [In, MarshalAs(UnmanagedType.LPArray)] IntPtr[] apidl, uint dwFlags);

[DllImport("shell32.dll", SetLastError = true)]
public static extern void SHParseDisplayName([MarshalAs(UnmanagedType.LPWStr)] string name, IntPtr bindingContext, [Out] out IntPtr pidl, uint sfgaoIn, [Out] out uint psfgaoOut);

public static void OpenFolderAndSelectItem(string folderPath, string file)
{
    IntPtr nativeFolder;
    uint psfgaoOut;
    SHParseDisplayName(folderPath, IntPtr.Zero, out nativeFolder, 0, out psfgaoOut);

    if (nativeFolder == IntPtr.Zero)
    {
        // Log error, can't find folder
        return;
    }

    IntPtr nativeFile;
    SHParseDisplayName(Path.Combine(folderPath, file), IntPtr.Zero, out nativeFile, 0, out psfgaoOut);

    IntPtr[] fileArray;
    if (nativeFile == IntPtr.Zero)
    {
        // Open the folder without the file selected if we can't find the file
        fileArray = new IntPtr[0];
    }
    else
    {
        fileArray = new IntPtr[] { nativeFile };
    }

    SHOpenFolderAndSelectItems(nativeFolder, (uint)fileArray.Length, fileArray, 0);

    Marshal.FreeCoTaskMem(nativeFolder);
    if (nativeFile != IntPtr.Zero)
    {
        Marshal.FreeCoTaskMem(nativeFile);
    }
}

답변

“/select,c:\file.txt”를 사용하십시오.

공백 대신 / select 뒤에 쉼표가 있어야합니다.