ASP.NET C# MVC Website, how can I mount a drive upon button click?

我的梦境 提交于 2020-01-05 05:31:16

问题


I am open to ways to solve this problem. I suspect the best way is to submit to the controller the path to be mounted and the controller will then pass back a python script that runs locally and mounts the path. Later we may need to verify Active Directory permissions but that's another question. We are able to configure all clients and servers as we wish, so somehow we should be able to allow the mount script to run after downloading. Only really concerned with mounting on windows but mac is optional. My main concern is how to get the server to send the script and get client to run the script, and if this is the right approach to satisfying this necessity; The second concern is how I form the path to access any arbitrary remote server share and the third concern is checking permissions first. Any help appreciated.

 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:


Final Answer to submit a script to the browser. User can also set the browser to open it automatically, python must also be installed and associated to .py files to allow quick double click to run. Not sure if that script/python mime is valid but it works. Improvements could include somehow using razor to modify a python script so that it isn't all inside quotes, losing color coding. Note you have to escape slashes twice recursively, one for the file stream and one for the script that gets downloaded and run.

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");
        }


来源:https://stackoverflow.com/questions/41088671/asp-net-c-sharp-mvc-website-how-can-i-mount-a-drive-upon-button-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!