怎么用C语言打开一个应用程序

怎么用C语言打开一个应用程序,第1张

默认路径是源文件的路径。

如果指定别的路径,两种方法

一是相对当前默认路径指定路径。

二是指定绝对路径。

譬如:

E:\C\c.c 这是源文件

E:\C\abc.exe

E:\D\abcd.exe

C:\A\aaa.exe

如果想执行abc.exe,那么可以不指定路径:

#include <stdlib.h>

int main()

{

char *cmd="abc.exe"

system(cmd)

return 0

}

如果想执行abcd.exe,那么可如下指定路径:

include <stdlib.h>

int main()

{

char *cmd="..\\D\\abcd.exe"

//指定相对路径

//char *cmd="E:\\D\\abcd.exe"

//指定绝对路径

system(cmd)

return 0

}

如果想执行aaa.exe,则必须指定绝对路径:

#include <stdlib.h>

int main()

{

char *cmd="C:\\A\\aaa.exe"

system(cmd)

return 0

}

对于空格问题,可按如下方法解决

#include <stdlib.h>

int main()

{

char *cmd="E:\\AB\" \"CD\\xyz.exe"

//即执行E:\AB CD\xyz.exe

system(cmd)

return 0

}

————————————————

在我机器上没问题,你是什么编译器?

一, 调用自己开发的应用

1)在plist文件中,注册对外接口

在xcode group&files 里面,展开 resources选择info.plist

鼠标右击information property list ,然后从列表中选择URL types

右击 add row 添加一个对象(item)右击item add row

从列表中选择 URL Schemes 再右击添加一个对象(item1)

将item1得值设置为:myapp

这个myapp就是对外接口,其它应用可以通过它,调用该应用

plist如下图所示:

2).处理URL请求

应用程序委托在 application:handleOpenURL:方法中处理传递给应用程序的URL请求。如果您已经为自己 的应用程序注册了定制的URL模式,则务必在委托中实现这个方法。

下面代码实现了这个委托方法;

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url

{

if([[url scheme] isEqualToString:@"myapp"]){

[application setApplicationIconBadgeNumber:10]

return YES

}

return NO

}

3).测试外部调用

1.回到Home屏幕,启动Safari(在iPhone仿真器上,在菜单上选择Hardware->Home命令就可以回到Home屏幕)。

2.在Safari的地址栏中,键入使用定制模式的URL: myapp:

3.确认您的应用程序是否启动,以及应用程序委托是否收到application:handleOpenURL:消息。

4)调用方法

在你需要调用上面注册过对外接口的应用中,添加下面代码即可

NSURL *url = [NSURL URLWithString:@"myapp:"]

[[UIApplication sharedApplication] openURL:url]

通过上述两个步骤,你可以在你的应用中,让用户打开你的其它应用

二, 调用IOS自带的应用

上面讲述的是调用自身的应用,讲解了如何在自己应用之间调用问题,今天介绍一下如果调用IOS自带的app的方法

一、调用app store界面方法

在实际开发中,往往要推荐自己其他应用和推荐自己的收费软件,那么我们就需要在程序中直接连接到app store的相应页面。

实际上的做法很简单,使用的还是UIApplication类的OpenURL方法:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"程序的相应连接"]]

二、调用其它应用的方法

前半部分是调用cmd.exe,后半部分是调用应用程序。

using System

using System.Diagnostics

namespace ApplyCmd

{

///

/// CmdUtility 的摘要说明。

///

public class CmdUtility

{

///

/// 执行cmd.exe命令

///

///命令文本

/// 命令输出文本

public static string ExeCommand(string commandText)

{

return ExeCommand(new string []{commandText})

}

///

/// 执行多条cmd.exe命令

///

///命令文本数组

/// 命令输出文本

public static string ExeCommand(string [] commandTexts)

{

Process p = new Process()

p.StartInfo.FileName = "cmd.exe"

p.StartInfo.UseShellExecute = false

p.StartInfo.RedirectStandardInput = true

p.StartInfo.RedirectStandardOutput = true

p.StartInfo.RedirectStandardError = true

p.StartInfo.CreateNoWindow = true

string strOutput = null

try

{

p.Start()

foreach(string item in commandTexts)

{

p.StandardInput.WriteLine(item)

}

p.StandardInput.WriteLine("exit")

strOutput = p.StandardOutput.ReadToEnd()

p.WaitForExit()

p.Close()

}

catch(Exception e)

{

strOutput = e.Message

}

return strOutput

}

///

/// 启动外部Windows应用程序,隐藏程序界面

///

///应用程序路径名称

/// true表示成功,false表示失败

public static bool StartApp(string appName)

{

return StartApp(appName,ProcessWindowStyle.Hidden)

}

///

/// 启动外部应用程序

///

///应用程序路径名称

///进程窗口模式

/// true表示成功,false表示失败

public static bool StartApp(string appName,ProcessWindowStyle style)

{

return StartApp(appName,null,style)

}

///

/// 启动外部应用程序,隐藏程序界面

///

///应用程序路径名称

///启动参数

/// true表示成功,false表示失败

public static bool StartApp(string appName,string arguments)

{

return StartApp(appName,arguments,ProcessWindowStyle.Hidden)

}

///

/// 启动外部应用程序

///

///应用程序路径名称

///启动参数

///进程窗口模式

/// true表示成功,false表示失败

public static bool StartApp(string appName,string arguments,ProcessWindowStyle style)

{

bool blnRst = false

Process p = new Process()

p.StartInfo.FileName = appName//exe,bat and so on

p.StartInfo.WindowStyle = style

p.StartInfo.Arguments = arguments

try

{

p.Start()

p.WaitForExit()

p.Close()

blnRst = true

}

catch

{

}

return blnRst

}

}

}

ps:利用System.Diagnostics.Process来压缩文件或文件夹

string strArg = "a -r {0} {1}"

System.Diagnostics.Process.Start(@"C:Program FilesWinRAR ar.exe", String.Format(strArg, txtApp.Text+".rar", txtApp.Text))

strArg为winrar的命令参数,请参考帮助。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/yw/11151106.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-14
下一篇2023-05-14

发表评论

登录后才能评论

评论列表(0条)

    保存