C# 操作設置共享文件夾
當前位置:點晴教程→知識管理交流
→『 技術文檔交流 』
/// <summary>
/// 共享文件操作類 /// </summary> public class SharedFolderManager { public SharedFolderManager() { // //TODO: 在此處添加構造函數(shù)邏輯 // } /// <summary> /// 共享文件夾根路徑 /// </summary> private static readonly string RootPath = ConfigurationManager.AppSettings["RootPath"]; /// <summary> /// 連接共享文件夾的賬號 /// </summary> private static readonly string User = ConfigurationManager.AppSettings["User"]; /// <summary> /// 連接共享文件夾的密碼 /// </summary> private static readonly string Password = ConfigurationManager.AppSettings["Password"]; /// <summary> /// 連接共享文件夾,連接上后可以像操作本地磁盤的方式操作文件夾和文件 /// 該方法使用配置文件中的信息連接共享文件夾 /// </summary> /// <returns></returns> public static bool Connect() { return Connect(RootPath, User, Password); } /// <summary> /// 連接共享文件夾,連接上后可以像操作本地磁盤的方式操作文件夾和文件 /// </summary> /// <param name="path">共享文件夾路徑</param> /// <param name="userName">用戶名</param> /// <param name="passWord">密碼</param> /// <returns>連接成功返回true,否則返回false</returns> public static bool Connect(string path, string userName, string passWord) { bool Flag = false; Process proc = new Process(); try { proc.StartInfo.FileName = "cmd.exe"; proc.StartInfo.UseShellExecute = false; proc.StartInfo.RedirectStandardInput = true; proc.StartInfo.RedirectStandardOutput = true; proc.StartInfo.RedirectStandardError = true; proc.StartInfo.CreateNoWindow = true; proc.Start(); string dosLine = @"net use " + path + " /User:" + userName + " " + passWord + " /PERSISTENT:YES"; proc.StandardInput.WriteLine(dosLine); proc.StandardInput.WriteLine("exit"); while (!proc.HasExited) { proc.WaitForExit(1000); } string errormsg = proc.StandardError.ReadToEnd(); proc.StandardError.Close(); if (string.IsNullOrEmpty(errormsg)) { Flag = true; } else { throw new Exception(errormsg); } } catch (Exception ex) { throw ex; } finally { proc.Close(); proc.Dispose(); } return Flag; } /// <summary> /// 相對共享文件夾根路徑的路徑 /// </summary> /// <param name="path"></param> /// <returns></returns> public static string GetPath(string path) { if (path.StartsWith("\\")) { path = path.Substring(1); } return Path.Combine(RootPath, path); } } 該文章在 2021/1/29 8:44:32 編輯過 |
關鍵字查詢
相關文章
正在查詢... |