简体   繁体   中英

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM