2011-08-23 5 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); 
     } 
    } 

업데이트

: 여기 위 호출하는 부분입니다. 내가 VS 2010 프로 (FYI) F5를 칠 때

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

, 그것은 컴파일 시작, 다음 Visual Studio에서이 IOException이이 처리되지 않은 오류가 날 수 있습니다 :

과정은 내가 '파일을 액세스 할 수 없습니다 : \ 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

왜 당신은 릴리스 모드에서 Debigging하고 있습니까? – 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

그게 전부입니다. 내가 질문을하자마자 나는 왜 그것이 그것을하고 있는지 깨달았지만, 나는 누군가가 그것을 짐작했는지 지켜 보았다. 게다가 이것은 앞으로 다른 사람들을 도울 수 있습니다. 나는 Length> 1에 대해 생각하지 않았다. –

0

Environment.GetCommandLineArgs의 첫 번째 인수()가 실행되고 실행됩니다. Environment.GetCommandLineArgs()를 전달하기 위해 Main 메서드를 수정해야합니다 .Controller.Run() 메서드를 건너 뛰고 (

)