我有一个案例,我想调用一个用R.Net Framework 4.5从R构建的C#控制台程序。在R中我使用system2
启动程序。在这个简单的演示案例中,控制台程序(C:\Temp\SettingsApp.exe
)只是从相应的.exe.config中读取一些设置并将它们打印到stdout。
我的问题是实际上没有读取配置文件,因为路径被错误地修改了。这是程序,配置和输出
using System;
using System.Configuration;
namespace SettingsApp {
class Program {
static void Main(string[] args) {
var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
Console.WriteLine(config.FilePath);
Console.WriteLine("Application: " + Properties.Settings.Default.MyAppSetting);
Console.WriteLine("User: " + Properties.Settings.Default.MyUserSetting);
}
}
}
此应用定义了两个设置,一个应用程序设置和一个用户设置并输出它们。另外,它输出配置文件的路径。配置文件(C:\Temp\SettingsApp.exe.config
)如下所示。
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="SettingsApp.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="SettingsApp.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<userSettings>
<SettingsApp.Properties.Settings>
<setting name="MyUserSetting" serializeAs="String">
<value>It's a user setting</value>
</setting>
</SettingsApp.Properties.Settings>
</userSettings>
<applicationSettings>
<SettingsApp.Properties.Settings>
<setting name="MyAppSetting" serializeAs="String">
<value>It's an app setting</value>
</setting>
</SettingsApp.Properties.Settings>
</applicationSettings>
</configuration>
用于构建SettingsApp(项目中的App.config)的配置文件的不同之处在于它使用其他值(MyUserSetting的“This is a user setting”和MyAppSetting的“This is a application setting”)。在R我然后打电话
system2("C:\\Temp\\SettingsApp.exe")
预期输出将是
C:\Temp\SettingsApp.exe.config
Application: It's an app setting
User: It's a user setting
但我在R控制台上的实际输出是
C:\Temp\SETTIN~1.EXE.config
Application: This is an application setting
User: This is a user setting
这些是项目的App.config中定义的设置的默认值。请注意,在第一行中,路径是8.3字符DOS文件名与最后添加的“.config”的奇怪组合。显然,这个文件不存在。因此我的假设是实际上没有读取配置文件:
>type C:\Temp\SETTIN~1.EXE.config
The system cannot find the file specified.
我有以下问题:
编辑:
我已经测试了here所示的变通方法,其中我重写了配置名称。这似乎有效,但并不是真正的解决方案。
我也注意到,例如Environment.GetCommandLineArgs()[0]
还显示了exe的8.3文件名。我看到路径的其余部分不是8.3,而是使用所有文件夹的全名。
我还测试了system
(同样的错误)和shell
(有效):
> shell(shQuote("C:\\Temp\\SettingsApp.exe"))
C:\Temp\SettingsApp.exe.Config
Application: It's an app setting
User: It's a user setting
我还测试了将可执行文件放在一个更长并且包含空格的路径下。可以看到只有文件名转换为8.3,而不是完整路径
> exe <- "C:\\Temp\\Longer More Complex Path\\SettingsApp.exe"
> system(shQuote(exe))
C:\Temp\Longer More Complex Path\SETTIN~1.EXE.config
Application: This is an application setting
User: This is a user setting
> system2(exe)
C:\Temp\Longer More Complex Path\SETTIN~1.EXE.config
Application: This is an application setting
User: This is a user setting
> shell(shQuote(exe))
C:\Temp\Longer More Complex Path\SettingsApp.exe.Config
Application: It's an app setting
User: It's a user setting
答案 0 :(得分:0)
<击>尝试击>
<击>$.ajax({
type: "GET",
headers: {
"Content-Type": "application/json"
},
cache: true,
url: "https://maps.googleapis.com/maps/api/place/autocomplete/json?INPUT_AND_API_KEY_AND_SOME_OTHER_PARAMS",
data: JSON.stringify(myData),
success: function(response) {
//code
}
});
击> <击>代替。这可能会阻止R破坏您的文件名。
这似乎是system2(shQuote("C:\\Temp\\SettingsApp.exe"))
和system
中的错误/限制。要解决此问题,您可以使用system2
来shell
间接运行您的程序。
cmd.exe