wpf遠程共用資料夾設定

來源:互聯網
上載者:User
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Windows;using System.Windows.Controls;using System.Windows.Data;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Imaging;using System.Windows.Navigation;using System.Windows.Shapes;using System.Diagnostics;using System.Management;using System.IO;using System.Security.AccessControl;namespace WpfApplication1{    /// <summary>    /// Window1.xaml 的互動邏輯    /// </summary>    public partial class Window1 : Window    {        public Window1()        {            InitializeComponent();        }        string gxDir = @"D:\楊磊";        //盤符映射        public string ConnectLan(string dx)        {            System.Diagnostics.Process _Process = new System.Diagnostics.Process();            _Process.StartInfo.FileName = "cmd.exe";            _Process.StartInfo.UseShellExecute = false;            _Process.StartInfo.RedirectStandardInput = true;            _Process.StartInfo.RedirectStandardOutput = true;            _Process.StartInfo.CreateNoWindow = true;            _Process.Start();            _Process.StandardInput.WriteLine(dx);            _Process.StandardInput.WriteLine("exit");            _Process.WaitForExit();            string _ReturnText = _Process.StandardOutput.ReadToEnd();// 得到cmd.exe的輸出              _Process.Close();            return _ReturnText;        }        //設定檔案夾共用        public static void ShareDirectory(string path, string ShareName, string Description)        {            ManagementClass _class = new ManagementClass(new ManagementPath("Win32_Share"));            object[] obj = { path, ShareName, 0, null, Description };            _class.InvokeMethod("Create", obj);        }        //設定Everyone許可權        public void SetAccessRole()        {            DirectoryInfo dInfo = new DirectoryInfo(gxDir);            DirectorySecurity dSecurity = dInfo.GetAccessControl();            InheritanceFlags iFlags = new InheritanceFlags();            iFlags = InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit;            FileSystemRights Rights = new FileSystemRights();            Rights = FileSystemRights.ReadData;            FileSystemAccessRule AccessRule2 = new FileSystemAccessRule("Everyone", Rights, iFlags, PropagationFlags.None, AccessControlType.Allow);            bool ok = false;            dSecurity.ModifyAccessRule(AccessControlModification.Add, AccessRule2, out ok);            dInfo.SetAccessControl(dSecurity);        }        //擷取映射盤符裡面的檔案        private void button3_Click(object sender, RoutedEventArgs e)        {            if (!System.IO.Directory.Exists(gxDir))            {                Directory.CreateDirectory(gxDir);            }            //設定共用資料夾            ShareDirectory(gxDir,"楊磊","yanglei");            //設定共用資料夾Everyone許可權            SetAccessRole();            //盤符映射            ConnectLan(@"net use l: \\192.168.0.152\ssss");//L:建立到本地的盤符,\\192.168.0.152\ssss共用資料夾路徑            //建立樹結構            DirectoryInfo _Direct = new DirectoryInfo(@"z:\");            TreeViewItem tvt = new TreeViewItem();            tvt.Header = "l:";            tvt.IsExpanded = true;            tr1.Items.Add(tvt);            //遞迴擷取映射盤符裡面所有檔案            getDir("l:\\", tvt);            //刪除映射關係            ConnectLan("net use l: /del");        }        //遞迴擷取映射盤符裡面所有檔案        public void getDir(string path, TreeViewItem tvt)        {            DirectoryInfo dir = new DirectoryInfo(path);            //迴圈擷取檔案夾            foreach (DirectoryInfo DirItem in dir.GetDirectories())            {                TreeViewItem tvt2 = new TreeViewItem();                tvt2.Header = DirItem.Name;                tvt2.IsExpanded = true;                tvt.Items.Add(tvt2);                string aa = DirItem.FullName.Substring(3);                string bendi = gxDir + aa;                if (!System.IO.Directory.Exists(bendi))                {                    Directory.CreateDirectory(bendi);                }                getDir(DirItem.FullName, tvt2);            }            //迴圈擷取檔案            foreach (FileInfo FileItem in dir.GetFiles())            {                TreeViewItem tvt1 = new TreeViewItem();                tvt1.Header = FileItem.Name;                tvt1.IsExpanded = true;                tvt.Items.Add(tvt1);                if (!System.IO.File.Exists(gxDir + FileItem.FullName.Substring(3)))                {                    FileStream fs = File.Create(gxDir + FileItem.FullName.Substring(3));                    fs.Flush();                    fs.Close();                }            }        }    }}win7上面要用Administrator登陸才行

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.