wpf远程共享文件夹设置
wpf远程共享文件夹设置
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 = "";
_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();// 得到的输出
_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登陆才行

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。