2011-08-23 18 views
0

我想獲得一個程序在當前實例中打開一個文件,而不是一個新的實例,這裏是我到目前爲止,我從this question單實例不能在C#中工作,程序無法訪問自己

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]); } 
      } 
     } 
    } 
} 

當我擊F5在VS 2010專業版(僅供參考),它編譯和啓動,然後給了我這個IOException異常沒有處理錯誤在Visual Studio:

該進程無法訪問該文件「我:\ 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() 
+0

什麼是堆棧跟蹤? – SLaks

+0

您是否檢查過使用可執行文件的進程? – StriplingWarrior

+0

你爲什麼在發行模式下進行重組? – Kiril

回答

1

不應該跳過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]); } 
      } 
     } 
    } 
+1

當然,你必須檢查args.Length> 1 – michalczerwinski

+0

就是這樣。關於一旦我問到這個問題,我意識到爲什麼這樣做,但我想我會繼續,看看有沒有人猜到它。此外,這可能會在未來幫助其他人。儘管我沒有考慮長度> 1。 –

0

在Environment.GetCommandLineArgs的第一個參數()是可執行正在運行。您需要修改您的Main方法以傳入Environment.GetCommandLineArgs()。跳過(1)到您的controller.Run()方法