ASP.NET C#MVC网站,如何在点击按钮时安装驱动器?

时间:2016-12-11 16:57:49

标签: c# asp.net asp.net-mvc mount

我愿意解决这个问题。我怀疑最好的方法是向控制器提交要安装的路径,然后控制器将传回一个本地运行的python脚本并安装路径。稍后我们可能需要验证Active Directory权限,但这是另一个问题。我们可以按照自己的意愿配置所有客户端和服务器,因此我们应该能够允许mount脚本在下载后运行。只关心安装在Windows上,但mac是可选的。我主要担心的是如何让服务器发送脚本并让客户端运行脚本,如果这是满足这种必要性的正确方法;第二个问题是我如何形成访问任意任意远程服务器共享的路径,第三个问题是首先检查权限。任何帮助表示赞赏。

 public ActionResult ProjectMountSubmit(string project_path, int project_number) {
            //Send mount script to user and make him run it
            return RedirectToAction("Project", "Home", new { ProjectNumber = project_number });
        }

1 个答案:

答案 0 :(得分:0)

最终答案将脚本提交给浏览器。用户还可以设置浏览器自动打开它,还必须安装python并将其与.py文件关联,以便快速双击运行。不确定该脚本/ python mime是否有效但它是否有效。改进可能包括使用razor修改python脚本,以便它不是所有内部引号,丢失颜色编码。请注意,您必须递归两次转义斜杠,一个用于文件流,另一个用于下载和运行的脚本。

public ActionResult ProjectMountSubmit(int project_number, string drive_letter) {
            ProjectsBAL b = new ProjectsBAL();
            Projects c = b.GetProject(project_number);
            //generate python mount script
            string script = "import ctypes\n" +
                            "import subprocess\n" +
                            "import os\n" +
                            "command = 'net use " + drive_letter + ": \\\\\\\\MSI\\\\Users\\\\gunslingor\\\\Desktop\\\\Storage\\\\" + c.CompanyName + "\\\\Clients\\\\" + c.ClientName + "\\\\" + c.ProjectNumber + "_" + c.ProjectName + "'\n" +
                            "returned = subprocess.Popen(command, shell = True, stdout = subprocess.PIPE, stderr = subprocess.PIPE)\n" +
                            "cmd_err_lines = returned.stderr.readlines()";

            var stream = new MemoryStream();
            StreamWriter writer = new StreamWriter(stream);
            writer.Write(script);
            writer.Flush();
            stream.Position = 0;
            //Send mount script to user, double click to run (python must be installed and associated to the file type py)
            return File(stream, "script/python", "Automount.py");
        }