[英]Escaping the path when launching an application in JavaScript via an ActiveX control
I want to send a file path to a .NET COM component via JavaScript without having to escape it in JavaScript. 我想通过JavaScript发送文件路径到.NET COM组件,而不必在JavaScript中转义它。
C:\\temp
you get the following... 当您通过类似C:\\temp
的路径时,将得到以下信息... \\\\Server\\Directory
you get the following... 当传递诸如\\\\Server\\Directory
类的UNC路径时,您将得到以下信息... C:\\\\temp
or \\\\\\\\Server\\\\Directory
) the value that is received is correct. 当您转义路径(例如C:\\\\temp
或\\\\\\\\Server\\\\Directory
)时,接收到的值是正确的。 @
in front of the variable name when storing to a private field. 存储到私有字段时,我尝试在变量名称前使用@
。 [Guid("30307EE0-82D9-4917-B07C-D3AB185FEF13")]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
[ComVisible(true)]
public interface ILauncher
{
[DispId(1)]
void setDirectory(string location);
[DispId(2)]
void launch(string args, bool debug = false);
}
[Guid("F91A7E9F-2397-4DEC-BDAD-EBFC65CFCCB2")]
[ProgId("MyActiveXControl.MyControl")]
[ClassInterface(ClassInterfaceType.None)]
[ComDefaultInterface(typeof(ILauncher))]
[ComVisible(true)]
public class Launcher : ILauncher
{
private string _location;
public void setDirectory(string location)
{
_location = location;
}
public void launch(string args, bool debug = false)
{
var programFiles = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86);
if (string.IsNullOrEmpty(programFiles))
{
programFiles = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
}
var exe = string.Format(@"{0}\MyApp\MyApp.exe", programFiles);
if (debug)
{
MessageBox.Show(exe, "Target Path", MessageBoxButtons.OK, MessageBoxIcon.Information);
MessageBox.Show(_location, "Drop Location", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
var startInfo = new ProcessStartInfo(exe, string.Format("\"{0}\" \"{1}\"", args, _location));
startInfo.WorkingDirectory = Path.GetDirectoryName(exe);
startInfo.CreateNoWindow = false;
try
{
var p = Process.Start(startInfo);
if (p == null)
{
MessageBox.Show("The app could not be started, please try again.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
<html>
<body>
<input type="button" value="Launch Control" onclick="launchControl();" />
<script>
function launchControl() {
o = new ActiveXObject("MyActiveXControl.MyControl");
o.setDirectory("C:\\temp");
o.launch("test_args", true);
}
</script>
</body>
</html>
In the end we found that we just needed to change the JavaScript because it can't be done later. 最后,我们发现我们只需要更改JavaScript,因为以后将无法完成。 By the time it gets to us we'd have to end up adding backslashes, but we wouldn't really be able to because we wouldn't know where one directory began and another ended. 等到到达我们的目录时,我们将不得不添加反斜杠,但实际上并不能,因为我们不知道一个目录从哪里开始而另一个目录从哪里结束。
Just modify the JavaScript. 只需修改JavaScript。
So we worked with the powers that be and all is well. 因此,我们与存在的力量合作,一切都很好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.