如何在c#.net中使用自定义扩展保存/打开自定义程序?

时间:2022-09-02 15:46:51

I am writing a small program using Windows Form Application in C#, .Net platform. Let's say I have made a paint app and want to save the picture I have done. How can I save the changes I made and open it up again so that I can change some of the parts using my program? Like the default Paint program in windows I want to save it and open it back again. It does not have to be a paint program or txt file, generally how can I save the work I have made in a custom program and save it with the extension of my program (let's say .paint).

我正在使用C#,。Net平台上的Windows Form Application编写一个小程序。假设我已经制作了一个绘画应用程序,并希望保存我已经完成的图片。如何保存我所做的更改并再次打开它,以便我可以使用我的程序更改某些部分?就像Windows中的默认Paint程序一样,我想保存它并再次打开它。它不一定是一个绘图程序或txt文件,一般如何保存我在自定义程序中所做的工作,并使用我的程序的扩展名保存(让我们说.paint)。

1 个解决方案

#1


0  

Two options:

  1. Write an installer which can associate your file extension with your program.
  2. 编写一个可以将文件扩展名与程序关联的安装程序。

  3. On your program start up check Windows registry if your file extension is associated with your program or not, if not then associate yourself so next time user simply click the file and your program automatically opens
  4. 在您的程序启动检查Windows注册表,如果您的文件扩展名与您的程序相关联,如果没有,然后关联自己,以便下次用户只需单击该文件,您的程序将自动打开

Sample code:

public static void SetAssociation(string Extension, string KeyName, string OpenWith, string FileDescription)
{
    CurrentUser = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\" + Extension, 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);

You need admin privileges to run this program.

您需要管理员权限才能运行此程序。

#1


0  

Two options:

  1. Write an installer which can associate your file extension with your program.
  2. 编写一个可以将文件扩展名与程序关联的安装程序。

  3. On your program start up check Windows registry if your file extension is associated with your program or not, if not then associate yourself so next time user simply click the file and your program automatically opens
  4. 在您的程序启动检查Windows注册表,如果您的文件扩展名与您的程序相关联,如果没有,然后关联自己,以便下次用户只需单击该文件,您的程序将自动打开

Sample code:

public static void SetAssociation(string Extension, string KeyName, string OpenWith, string FileDescription)
{
    CurrentUser = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\" + Extension, 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);

You need admin privileges to run this program.

您需要管理员权限才能运行此程序。