C# - 파일 확장자에 연결된 프로그램을 등록하는 방법 (2) - 웹 브라우저가 다운로드 후 자동 실행
지난번에 연결한 파일 확장자(.1myext)에 해당하는 파일을,
C# - 파일 확장자에 연결된 프로그램을 등록하는 방법 (1) - 기본
; https://www.sysnet.pe.kr/2/0/10966
IIS 웹 서버에 올려놓고 web.config에 웹 브라우저가 다운로드할 수 있도록 확장자를 연결해 줍니다.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<staticContent>
<mimeMap fileExtension=".1myext" mimeType="application/octet-stream" />
</staticContent>
</system.webServer>
</configuration>
이제 웹 브라우저에서 해당 파일을 열면 다음과 같이 다운로드 되었음을 알리는 창이 뜹니다.
그런데, 웹 브라우저가 test.1myext 파일을 다운로드 받았으면 곧바로 우리가 등록한 프로그램을 실행해 주면 더 좋지 않을까요? 이를 위해 확장자 연결 프로그램 등록 시 "EditFlags" 값을 설정해 주시면 됩니다.
FILETYPEATTRIBUTEFLAGS
; https://docs.microsoft.com/en-us/windows/win32/api/shlwapi/ne-shlwapi-filetypeattributeflags
Configuring Windows Explorer - Registry EditFlags
; http://mc-computing.com/winexplorer/WinExplorerEditFlags.htm
다양한 EditFlags 값 중에서 웹 브라우저에 안전하게 열 수 있도록 지정하는 값은 FTA_OpenIsSafe(0x00010000) 입니다.
FTA_OpenIsSafe - 0x00010000
Indicates that the file type's open verb can be safely invoked for downloaded files. This flag applies only to safe file types, as identified by
AssocIsDangerous. To improve the user experience and reduce unnecessary user prompts when downloading and activating items, file type owners should specify this flag and applications that download and activate files should respect this flag.
그럼, 지난 예제 프로젝트에 관련 코드를 추가해야겠지요.
private static void ProcessFileExtReg(bool register)
{
using (RegistryKey classesKey = Registry.CurrentUser.OpenSubKey(@"Software\Classes", true))
{
if (register == true)
{
// ...[생략]...
using (RegistryKey typeKey = classesKey.CreateSubKey(extType))
{
typeKey.SetValue(null, fileTypeDesc);
typeKey.SetValue("EditFlags", new byte[] { 00, 00, 01, 00 }, RegistryValueKind.Binary);
// ...[생략]...
}
}
// ...[생략]...
}
}
이렇게 등록한 후, 다시 웹 브라우저에서 해당 파일을 주소창에 치고 들어가면 이번에는 다운로드 알림 창이 아닌, 다음과 같이 연결 프로그램이 직접 실행된 것을 볼 수 있습니다.
[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]