PDA

View Full Version : چگونه کاری کنیم که فایل هایی با پسوند خاص فقط توسط برنامه ما قابل اجرا باشد؟ File Association



4EBRAHIM4
پنج شنبه 08 آبان 1393, 13:36 عصر
سلام دوستان من دوست دارم برنامه بنویسم که وقتی به وسیله اون یک فایل با پسوند خاصی ایجاد می کنم فقط برنامه ی من قادر به باز کردنش باشه و با notpad باز نشه در ضمن فایل های ایجاد شده ایکون مخصوص خودشون رو داشته باشن مثلا فایل های psd که توسط فوتوشاپ ایجاد میشه و ایکون مخصوصی هم داره

FastCode
پنج شنبه 08 آبان 1393, 20:47 عصر
...

http://stackoverflow.com/questions/7428784/how-can-i-associate-a-specific-file-extension-with-my-application


public static void SetAssociation(string Extension, string KeyName, string OpenWith, string FileDescription)
{
// The stuff that was above here is basically the same

// Delete the key instead of trying to change it
CurrentUser = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Expl orer\\FileExts\\.ucs", true);
CurrentUser.DeleteSubKey("UserChoice", false);
CurrentUser.Close();

// Tell explorer the file association has been changed
SHChangeNotify(0x08000000, 0x0000, IntPtr.Zero, IntPtr.Zero);
}

[DllImport("shell32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern void SHChangeNotify(uint wEventId, uint uFlags, IntPtr dwItem1, IntPtr dwItem2);
public static void SetAssociation(string Extension, string KeyName, string OpenWith, string FileDescription)
{
RegistryKey BaseKey;
RegistryKey OpenMethod;
RegistryKey Shell;
RegistryKey CurrentUser;

BaseKey = Registry.ClassesRoot.CreateSubKey(Extension);
BaseKey.SetValue("", KeyName);

OpenMethod = Registry.ClassesRoot.CreateSubKey(KeyName);
OpenMethod.SetValue("", FileDescription);
OpenMethod.CreateSubKey("DefaultIcon").SetValue("", "\"" + OpenWith + "\",0");
Shell = OpenMethod.CreateSubKey("Shell");
Shell.CreateSubKey("edit").CreateSubKey("command").SetValue("", "\"" + OpenWith + "\"" + " \"%1\"");
Shell.CreateSubKey("open").CreateSubKey("command").SetValue("", "\"" + OpenWith + "\"" + " \"%1\"");
BaseKey.Close();
OpenMethod.Close();
Shell.Close();

CurrentUser = Registry.CurrentUser.CreateSubKey(@"HKEY_CURRENT_USER\Software\Microsoft\Windows\Curre ntVersion\Explorer\FileExts\.ucs");
CurrentUser = CurrentUser.OpenSubKey("UserChoice", RegistryKeyPermissionCheck.ReadWriteSubTree, System.Security.AccessControl.RegistryRights.FullC ontrol);
CurrentUser.SetValue("Progid", KeyName, RegistryValueKind.String);
CurrentUser.Close();
}
source:http://stackoverflow.com/questions/2681878/associate-file-extension-with-application/2682083