Asp.net Access To Network Share

后端 未结 3 520
我在风中等你
我在风中等你 2020-12-01 15:53

I have an ASP.NET app which needs to save files to a network share (Samba).

The share requires a username and password to connect.

I have mapped a persistent

3条回答
  •  甜味超标
    2020-12-01 15:53

    Did you try mapping the drive in code? Here is a class for doing just that...

    public static class NetworkDrives
        {
            public static bool  MapDrive(string DriveLetter, string Path, string Username, string Password)
            {
    
                bool ReturnValue = false;
    
                if(System.IO.Directory.Exists(DriveLetter + ":\\"))
                {
                    DisconnectDrive(DriveLetter);
                }
                System.Diagnostics.Process p = new System.Diagnostics.Process();
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.CreateNoWindow = true;
                p.StartInfo.RedirectStandardError = true;
                p.StartInfo.RedirectStandardOutput = true;
    
                p.StartInfo.FileName = "net.exe";
                p.StartInfo.Arguments = " use " + DriveLetter + ": " + Path + " " + Password + " /user:" + Username;
                p.Start();
                p.WaitForExit();
    
                string ErrorMessage = p.StandardError.ReadToEnd();
                string OuputMessage = p.StandardOutput.ReadToEnd();
                if (ErrorMessage.Length > 0)
                {
                    throw new Exception("Error:" + ErrorMessage);
                }
                else
                {
                    ReturnValue = true;
                }
                return ReturnValue;
            }
            public static bool DisconnectDrive(string DriveLetter)
            {
                bool ReturnValue = false;
                System.Diagnostics.Process p = new System.Diagnostics.Process();
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.CreateNoWindow = true;
                p.StartInfo.RedirectStandardError = true;
                p.StartInfo.RedirectStandardOutput = true;
    
                p.StartInfo.FileName = "net.exe";
                p.StartInfo.Arguments = " use " + DriveLetter + ": /DELETE";
                p.Start();
                p.WaitForExit();
    
                string ErrorMessage = p.StandardError.ReadToEnd();
                string OuputMessage = p.StandardOutput.ReadToEnd();
                if (ErrorMessage.Length > 0)
                {
                    throw new Exception("Error:" + ErrorMessage);
                }
                else
                {
                    ReturnValue = true;
                }
                return ReturnValue;
            }
    
        }
    

提交回复
热议问题