성태의 닷넷 이야기
홈 주인
모아 놓은 자료
프로그래밍
질문/답변
사용자 관리
사용자
메뉴
아티클
외부 아티클
유용한 코드
온라인 기능
MathJax 입력기
최근 덧글
[정성태] Java - How to use the Foreign Funct...
[정성태] 제가 큰 실수를 했군요. ^^; Delegate를 통한 Bein...
[정성태] Working with Rust Libraries from C#...
[정성태] Detecting blocking calls using asyn...
[정성태] 아쉽게도, 커뮤니티는 아니고 개인 블로그입니다. ^^
[정성태] 질문이 잘 이해가 안 됩니다. 우선, 해당 소스코드에서 ILis...
[양승조
] var대신 dinamic으로 선언해서 해결은 했습니다. 맞는 해...
[양승조
] 또 막혔습니다. ㅠㅠ var list = props[i].Ge...
[양승조
] 아. 감사합니다. 어제는 안됐던것 같은데....정신을 차려야겠네...
[정성태] "props[i].GetValue(props[i])" 코드에서 ...
글쓰기
제목
이름
암호
전자우편
HTML
홈페이지
유형
제니퍼 .NET
닷넷
COM 개체 관련
스크립트
VC++
VS.NET IDE
Windows
Team Foundation Server
디버깅 기술
오류 유형
개발 환경 구성
웹
기타
Linux
Java
DDK
Math
Phone
Graphics
사물인터넷
부모글 보이기/감추기
내용
<div style='display: inline'> <h1 style='font-family: Malgun Gothic, Consolas; font-size: 20pt; color: #006699; text-align: center; font-weight: bold'>C# - 파일 확장자에 연결된 프로그램을 등록하는 방법 (1) - 기본</h1> <p> 특정 확장자와 응용 프로그램을 연결하는 방법은 레지스트리를 통해서 이뤄집니다. 다음의 글에도 잘 나오는데요.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > Create registry entry to associate file extension with application in C++ ; <a target='tab' href='http://stackoverflow.com/questions/1387769/create-registry-entry-to-associate-file-extension-with-application-in-c'>http://stackoverflow.com/questions/1387769/create-registry-entry-to-associate-file-extension-with-application-in-c</a> </pre> <br /> 이번 글에서는 가장 단순한 방법만 설명할텐데, 그러니까 아래와 같이 딱 2개의 레지스트리만 등록해 주면 됩니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > Windows Registry Editor Version 5.00 [HKEY_CURRENT_USER\Software\Classes\blergcorp.blergapp.v1\shell\open\command] @="c:\path\to\app.exe \"%1\"" [HKEY_CURRENT_USER\Software\Classes\.blerg] @="blergcorp.blergapp.v1" </pre> <br /> C# 코드로 보면 대략 다음과 같이 구현해 줄 수 있습니다.<br /> <br /> <pre style='height: 400px; margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > using System.IO; using Microsoft.Win32; namespace YourExtRegister { class Program { static string ext = ".1myext"; static string fileTypeDesc = "my ext sample"; static string extType = "yourext" + ext + ".v1"; static string assocExeFileName = "YourExt.exe"; static void Main(string[] args) { bool register = true; if (args.Length >= 1) { if (args[0] == "-u") { register = false; } } ProcessFileExtReg(register); } private static void ProcessFileExtReg(bool register) { using (RegistryKey classesKey = Registry.CurrentUser.OpenSubKey(@"Software\Classes", true)) { if (register == true) { using (RegistryKey extKey = classesKey.CreateSubKey(ext)) { extKey.SetValue(null, extType); } // or, use Registry.SetValue method using (RegistryKey typeKey = classesKey.CreateSubKey(extType)) { typeKey.SetValue(null, fileTypeDesc); using (RegistryKey shellKey = typeKey.CreateSubKey("shell")) { using (RegistryKey openKey = shellKey.CreateSubKey("open")) { using (RegistryKey commandKey = openKey.CreateSubKey("command")) { string assocExePath = GetProcessPath(); string assocCommand = string.Format("\"{0}\" \"%1\"", assocExePath); commandKey.SetValue(null, assocCommand); } } } } } else { DeleteRegistryKey(classesKey, ext, false); DeleteRegistryKey(classesKey, extType, true); } } } private static void DeleteRegistryKey(RegistryKey classesKey, string subKeyName, bool deleteAllSubKey) { if (CheckRegistryKeyExists(classesKey, subKeyName) == false) { return; } if (deleteAllSubKey == true) { classesKey.DeleteSubKeyTree(subKeyName); } else { classesKey.DeleteSubKey(subKeyName); } } private static bool CheckRegistryKeyExists(RegistryKey classesKey, string subKeyName) { RegistryKey regKey = null; try { regKey = classesKey.OpenSubKey(subKeyName); return regKey != null; } finally { if (regKey != null) { regKey.Close(); } } } private static string GetProcessPath() { string path = Path.GetDirectoryName(typeof(Program).Assembly.Location); return Path.Combine(path, assocExeFileName); } } } </pre> <br /> 참고로, 레지스트리가 아닌 윈도우 제어판에서 "Default Programs"를 선택해 "Associate a file type or protocol with a program" 링크를 통해서도 다음과 같이 확인할 수 있습니다.<br /> <br /> <img alt='yourext_sample_1.png' src='/SysWebRes/bbs/yourext_sample_1.png' /><br /> <br /> 이제부터는, (예제에서는 확장자가 .1myext 이므로) test.1myext와 같은 파일을 탐색기에서 더블 클릭하면 등록된 EXE 프로그램이 실행됩니다. 그리고, 그 프로그램에서는 넘겨진 인자 정보를 통해 파일을 처리해 주시면 됩니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > using System.Windows.Forms; namespace YourExt { class Program { static void Main(string[] args) { if (args.Length < 1) { return; } MessageBox.Show("Viewer Runs: " + args[0]); } } } </pre> <br /> (<a target='tab' href='http://www.sysnet.pe.kr/bbs/DownloadAttachment.aspx?fid=1027&boardid=331301885'>첨부한 파일은 이 글의 예제 코드를 포함</a>합니다.)<br /> </p><br /> <br /><hr /><span style='color: Maroon'>[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]</span> </div>
첨부파일
스팸 방지용 인증 번호
3726
(왼쪽의 숫자를 입력해야 합니다.)