application)Myprogram.cs:staticclassProgram{[DllImport("user32.dll")]staticexternIntPtrFindWindow(stringlpClassName,stringlpWindowName);[DllImport("user32.dll")]staticexternIntPtrSendMessage(IntPtrhWnd,uintMsg,UIntPtrwParam,IntPtrlParam);[DllImport("user32.dll",SetLastError=true,CharSet=CharSet.Auto)]staticexternuintRegisterWindowMessage(stringlpString);[STAThread]staticvoidMain(paramsstring[]Arguments){if(Arguments.Length>0){//这意味着单击了上下文菜单中的上传项//这里是方法“uploadImage(stringlocation)”//必须运行正在运行的应用程序}else{//只需启动应用程序Application.Run(newControlPanel());}}}请注意,ControlPanel类没有可见的表单,只有托盘图标,因为不需要表单。我能帮忙解决这个问题吗?我已经弄清楚了,非常感谢发布链接http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/a5bcfc8a-bf69-4bbc-923d-f30f9ecf5f64的人,这正是我在找什么!这里有一个完整的解决方案:staticclassProgram{[STAThread]staticvoidMain(paramsstring[]Arguments){}publicstaticvoidNewInstanceHandler(objectsender,StartupNextInstanceEventArgse){stringimageLocation=e.CommandLine[1];MessageBox.Show(imageLocation);e.BringToForeground=false;ControlPanel.uploadImage(imageLocation);}publicclassSingleInstanceApplication:WindowsFormsApplicationBase{privateSingleInstanceApplication(){base.IsSingleInstance=true;publicstaticvoidRun(Formf,StartupNextInstanceEventHandlerstartupHandler){SingleInstanceApplicationapp=newSingleInstanceApplication();app.MainForm=f;app.StartupNextInstance+=startupHandler;app.Run(Environment.GetCommandLineArgs());,特别是发布我上面提到的链接的人,但我猜他删除了他的答案?此致,Kenny那么您将必须为其他应用程序设置一个通信通道来发布图像这个通信通道可以是以下之一-不是完整列表只是一个示例:如您所见,有几种可能性。哪一种适合您取决于您??的情况。文件系统是一个可以使用FileSystemWatcher轻松实现的选项,请参见此处。自托管Web服务公开了可以接收图像的Web服务。请在此处查看示例。恕我直言,这两个选项是最简单的。但是……还有一些。对于TCP端口,请参阅Tim的帖子。假设您可以控制执行环境,监听应用程序可以使用WCF或什至原始TCP套接字公开端点。这样,任何其他应用程序都可以以动态但结构化的方式连接到它。即使发送方和接收方在同一台机器上,使用WCF或TCP等网络传输解决方案也是跨进程安全发送数据的好方法。下面是使用c#在TCP中执行此操作的示例:http://www.switchonthecode.com/tutorials/csharp-tutorial-simple-threaded-tcp-serverWCF可能有点复杂(部分原因是它的灵活性,也由于到序列化限制),但是有很多关于如何使用它的在线文档。WCF是一种更面向对象的解决方案,因为可以生成代理类,允许您对实际对象进行强类型调用,而不仅仅是发送消息。我在之前的解决方案中添加了一些小的补充来引用表单上的设置器,以便可以将参数传递给它。首先,创建对窗体初始实例(MainForm)的静态引用。然后,当随后发送参数时,NewInstanceHandler可以使用保存的表单引用来访问其公共方法/属性(在我的例子中,一个名为AddItem的设置器)。一个简单的测试方法是向表单添加一个公共属性,并使用设置器更改表单的文本属性(标题文本)。[STAThread]静态Form1主窗体;staticvoidMain(paramsstring[]Arguments){Application.EnableVisualStyles();Application.SetCompatibleTextRenderingDefault(false);主窗体=新窗体1();SingleInstanceApplication.Run(MainForm,NewInstanceHandler);}publicstaticvoidNewInstanceHandler(objectsender,StartupNextInstanceEventArgse){MainForm.AddItem=e.CommandLine[1];e.BringToForeground=false;}publicclassSingleInstanceApplication:WindowsFormsApplicationBase{privateSingleInstanceApplication(){base.IsvoSingleInstance}public=true;(Formf,StartupNextInstanceEventHandlerstartupHandler){SingleInstanceApplicationapp=newSingleInstanceApplication();app.MainForm=f;app.StartupNextInstance+=startupHandler;app.Run(Environment.GetCommandLineArgs());要在提供现有实例后运行第二个实例,我在下面添加了代码片段。以上就是《C#学习教程:传递参数运行应用》分享的全部内容。如果对大家有用,需要进一步了解C#学习教程,希望大家多加关注—staticclassProgram{[STAThread]staticvoidMain(paramsstring[]Arguments){Form1MainForm;布尔bInstanceFlag;MutexMyApplicationMutex=newMutex(true,"MyApp_Mutex",outbInstanceFlag);Application.EnableVisualStyles();Application.SetCompatibleTextRenderingDefault(false);如果(!bInstanceFlag){MainForm=newForm1();SingleInstanceApplication.Run(MainForm,NewInstanceHandler);}else{MainForm=newForm1();SingleInstanceApplication.Run(MainForm,NewInstanceHandler);主窗体.关闭();}}publicstaticvoidNewInstanceHandler(objectsender,StartupNextInstanceE){MainForm.AddItem=e.CommandLine[1];e.BringToForeground=false;}publicclassSingleInstanceApplication:WindowsFormsApplicationBase{privateSingleInstanceApplication(){base.IsSingleInstance=true;}publicstaticvoidRun(Formf,StartupNextInstancestartupHandler){唱leInstanceApplicationapp=newSingleInstanceApplication();app.MainForm=f;app.StartupNextInstance+=startupHandler;app.Run(Environment.GetCommandLineArgs());联系管理员删除。如需转载请注明出处:
