1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
|
public string MakeShare(string shareName,
string path,
string accessRights,
int? numberOfUsers = null,
string cacheType = null)
{
string returnStr = null;
try
{
if ((string.IsNullOrEmpty(shareName) == false) & (string.IsNullOrEmpty(path) == false))
{
arguments = string.Format("share {0}={1} {2}{3}{4}",
shareName,
path,
(accessRights != null) ? accessRights : "",
(numberOfUsers == 0) ? " /UNLIMITED " : " /USERS:" + numberOfUsers.ToString(),
(cacheType != null) ? " /CACHE:" + cacheType : "");
returnStr = this.LaunchProcess(Environment.SystemDirectory, "net.exe",arguments);
}
else
System.Windows.MessageBox.Show("shareName and path can't be null or empty", "Parameter missing", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error);
return returnStr;
}
catch (Exception)
{
return null;
}
}
public string LaunchProcess(string workingDirectory, string software, string arguments)
{
string returnStr = null;
try
{
Process proc = new Process();
proc.StartInfo.WorkingDirectory = workingDirectory;
proc.StartInfo.FileName = software;
proc.StartInfo.Arguments = arguments;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.CreateNoWindow = true;
proc.Start();
proc.WaitForExit();
returnStr = proc.StandardOutput.ReadToEnd();
proc.Close();
}
catch (Exception) { return null; }
return returnStr;
} |
Partager