单实例在 C# 中不起作用,程序无法访问自身

发布于 2024-11-30 19:52:36 字数 4607 浏览 1 评论 0原文

我正在尝试让程序在当前实例而不是新实例中打开文件,这是到目前为止我所拥有的,这是我从 这个问题

static class Program
    {
        static EsfEditorSingleton controller;
        [STAThread]
        public static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            // Show the main form of the app.
            controller = new EsfEditorSingleton();
            string[] args = Environment.GetCommandLineArgs();
            controller.Run(args);
            controller.StartupNextInstance += controller.NextInstanceStartup;
        }

    }
    public class EsfEditorSingleton : WindowsFormsApplicationBase
    {
        internal EsfEditorSingleton() : base()
        {
            this.IsSingleInstance = true;
            this.MainForm = new EsfEditorForm();
        }
        public void NextInstanceStartup(object sender, StartupNextInstanceEventArgs e)
        {
            EsfEditorForm form = (EsfEditorForm)this.MainForm;
            string[] args = null;
            e.CommandLine.CopyTo(args, 0);
            form.mProcessArgs(args);
        }
    }

更新:这是上面调用的部分。

public class EsfEditorForm : Form
{

    public EsfEditorForm()
    {
        this.InitializeComponent();
    }

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        string[] args = Environment.GetCommandLineArgs();
        mProcessArgs(args);
    }

    public void mProcessArgs(string[] args)
    {
        if (args.Length > 0)
        {
            for (int i = 0; i < args.Length; i++)
            {
                if (System.IO.File.Exists(args[i])) { this.OpenFile(args[i]); }
            }
        }
    }
}

当我在 VS 2010 Pro 中按 F5(仅供参考)时,它会编译并启动,然后在 Visual Studio 中给出此 IOException 未处理错误:

该进程无法访问文件 'I:\Program Files\Totar\EFS Editor\VS - Solution\bin\x86\Release\EsfEditor 1.4.8.exe',因为它正被另一个进程使用。

我相信提到的文件是当前正在运行的可执行文件。

堆栈跟踪

at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
at System.IO.File.Open(String path, FileMode mode, FileAccess access, FileShare share)
at EsfEditor.Parser.EsfParser..ctor(String filename)
at EsfEditor.Core.EsfObjects.EsfFile..ctor(String filePath)
at EsfEditor.Forms.EsfEditorForm.OpenFile(String filePath)
at EsfEditor.Forms.EsfEditorForm.mProcessArgs(String[] args)
at EsfEditor.Forms.EsfEditorForm.OnLoad(EventArgs e)
at System.Windows.Forms.Form.OnCreateControl()
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.WmShowWindow(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ContainerControl.WndProc(Message& m)
at System.Windows.Forms.Form.WmShowWindow(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.SafeNativeMethods.ShowWindow(HandleRef hWnd, Int32 nCmdShow)
at System.Windows.Forms.Control.SetVisibleCore(Boolean value)
at System.Windows.Forms.Form.SetVisibleCore(Boolean value)
at System.Windows.Forms.Control.set_Visible(Boolean value)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(ApplicationContext context)
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
at EsfEditor.Program.Main()

I am trying to get a program to open a file in the current instance rather than a new instance, and here is what I have so far, which I got from this question.

static class Program
    {
        static EsfEditorSingleton controller;
        [STAThread]
        public static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            // Show the main form of the app.
            controller = new EsfEditorSingleton();
            string[] args = Environment.GetCommandLineArgs();
            controller.Run(args);
            controller.StartupNextInstance += controller.NextInstanceStartup;
        }

    }
    public class EsfEditorSingleton : WindowsFormsApplicationBase
    {
        internal EsfEditorSingleton() : base()
        {
            this.IsSingleInstance = true;
            this.MainForm = new EsfEditorForm();
        }
        public void NextInstanceStartup(object sender, StartupNextInstanceEventArgs e)
        {
            EsfEditorForm form = (EsfEditorForm)this.MainForm;
            string[] args = null;
            e.CommandLine.CopyTo(args, 0);
            form.mProcessArgs(args);
        }
    }

Update: Here is the part that the above calls.

public class EsfEditorForm : Form
{

    public EsfEditorForm()
    {
        this.InitializeComponent();
    }

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        string[] args = Environment.GetCommandLineArgs();
        mProcessArgs(args);
    }

    public void mProcessArgs(string[] args)
    {
        if (args.Length > 0)
        {
            for (int i = 0; i < args.Length; i++)
            {
                if (System.IO.File.Exists(args[i])) { this.OpenFile(args[i]); }
            }
        }
    }
}

When I hit F5 in VS 2010 Pro (FYI), it compiles and starts, and then gives me this IOException was not handled error in Visual Studio:

The process cannot access the file 'I:\Program Files\Totar\EFS Editor\VS - Solution\bin\x86\Release\EsfEditor 1.4.8.exe' because it is being used by another process.

I believe the file mentioned is the executable that is running at the moment.

StackTrace:

at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
at System.IO.File.Open(String path, FileMode mode, FileAccess access, FileShare share)
at EsfEditor.Parser.EsfParser..ctor(String filename)
at EsfEditor.Core.EsfObjects.EsfFile..ctor(String filePath)
at EsfEditor.Forms.EsfEditorForm.OpenFile(String filePath)
at EsfEditor.Forms.EsfEditorForm.mProcessArgs(String[] args)
at EsfEditor.Forms.EsfEditorForm.OnLoad(EventArgs e)
at System.Windows.Forms.Form.OnCreateControl()
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.WmShowWindow(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ContainerControl.WndProc(Message& m)
at System.Windows.Forms.Form.WmShowWindow(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.SafeNativeMethods.ShowWindow(HandleRef hWnd, Int32 nCmdShow)
at System.Windows.Forms.Control.SetVisibleCore(Boolean value)
at System.Windows.Forms.Form.SetVisibleCore(Boolean value)
at System.Windows.Forms.Control.set_Visible(Boolean value)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(ApplicationContext context)
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
at EsfEditor.Program.Main()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雨后咖啡店 2024-12-07 19:52:36

你不应该跳过 mProcessArgs 中的第一个参数吗?

public void mProcessArgs(string[] args)
    {
        if (args.Length > 0)
        {
            for (int i = **1**; i < args.Length; i++)
            {
                if (System.IO.File.Exists(args[i])) { this.OpenFile(args[i]); }
            }
        }
    }

Shouldn't you skip first parameter in mProcessArgs?

public void mProcessArgs(string[] args)
    {
        if (args.Length > 0)
        {
            for (int i = **1**; i < args.Length; i++)
            {
                if (System.IO.File.Exists(args[i])) { this.OpenFile(args[i]); }
            }
        }
    }
水溶 2024-12-07 19:52:36

Environment.GetCommandLineArgs() 中的第一个参数是正在运行的可执行文件。您需要修改 Main 方法以将Environment.GetCommandLineArgs().Skip(1) 传递给您的controller.Run() 方法

The first argument in Environment.GetCommandLineArgs() is the executable being run. You need to modify your Main method to pass in Environment.GetCommandLineArgs().Skip(1) to your controller.Run() method

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文