ImageToAccess/Program.cs

来源:互联网 发布:程序员修炼之道pdf网盘 编辑:程序博客网 时间:2024/05/22 14:06

using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
using Microsoft.Win32;
using Access = Microsoft.Office.Interop.Access;

namespace ImageAccess
{
    static class Program
    {
        #region DllImportAttribute
        [DllImport("user32.dll", EntryPoint = "ShowWindow")]
        static extern bool ShowWindow(IntPtr handle, int flags);

        [DllImport("user32.dll", EntryPoint = "SetForegroundWindow")]
        static extern bool SetForegroundWindow(IntPtr handle);
        #endregion

        [STAThread]
        static void Main()
        {
            #region Mutex
            bool isCreated; // 互斥体名称须唯一。
            using (Mutex newMutex = new Mutex(true, @"Local/ImageAccess", out isCreated))
            {
                if (isCreated)
                {
                    string dbPath = Path.Combine(Application.StartupPath, "Images.mdb");
                    if (File.Exists(dbPath))
                    {
                        Application.EnableVisualStyles();
                        Application.SetCompatibleTextRenderingDefault(false);
                        using (RegistryKey subKey = Application.UserAppDataRegistry)
                        {
                            FormImageAccess frame = new FormImageAccess();
                            subKey.SetValue("Handle", frame.Handle);
                            Application.Run(frame);
                        }
                        newMutex.ReleaseMutex(); // 释放互斥体的所属权。
                    }
                    else
                    {
                        Access.Application newAccess = new Access.Application();
                        newAccess.NewCurrentDatabase(dbPath);
                        dao.Database db = newAccess.CurrentDb();
                        db.Execute("create table [Images] (Name Text primary key, Bytes Image)", Type.Missing);
                        db.NewPassword("", "jinzhexian");
                        db.Close();
                        newAccess.Quit(Access.AcQuitOption.acQuitSaveNone);
                        newAccess = null;
                        GC.Collect();
                        Application.ExitThread();
                    }
                }
                else
                {
                    string text = string.Format("“{0}”应用程序已经运行。", AppDomain.CurrentDomain.FriendlyName);
                    MessageBox.Show(text, "系统提示!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    using (RegistryKey subKey = Application.UserAppDataRegistry)
                    {
                        IntPtr handle = new IntPtr(Convert.ToInt32(subKey.GetValue("Handle")));
                        ShowWindow(handle, 1);
                        SetForegroundWindow(handle);
                    }
                    Application.ExitThread();
                }
            }
            #endregion
        }
    }
}