NEWS

[C#] Hướng dẫn chia sẽ share thư mục folder trong winform

[C#] Hướng dẫn chia sẽ share thư mục folder trong winform
Đăng bởi: Thảo Meo - Lượt xem: 5955 11:25:26, 23/04/2019C#   In bài viết

Xin chào các bạn, bài viết hôm nay, mình sẽ tiếp tục hướng dẫn các bạn cách chia sẽ thư mục (share folder) trong lập trình ngôn ngữ C#.

[C#] Share folder in winform

Để ứng dụng C# của bạn chia sẽ được thư mục, các bạn cần chạy ứng dụng ở chế độ Run Administator mới chạy share được nhé các bạn.

Để ứng dụng chạy ở chế độ Admin các bạn tham khảo ở bài viết này:

[C#] Hướng dẫn chạy ứng dụng dưới quyền Administrator trong lập trình csharp

share_folder_csharp

Đầu tiên các bạn cần import thư viện System.Management vào project.

Tiếp theo, các bạn viết cho mình hàm ShareFolderPermission() để share thư mục.

Source code ShareFolderPermission() c#:

public void ShareFolderPermission(string FolderPath, string ShareName, string Description)
{
    try
    {
        // Calling Win32_Share class to create a shared folder
        ManagementClass managementClass = new ManagementClass("Win32_Share");
        // Get the parameter for the Create Method for the folder
        ManagementBaseObject inParams = managementClass.GetMethodParameters("Create");
        ManagementBaseObject outParams;
        // Assigning the values to the parameters
        inParams["Description"] = Description;
        inParams["Name"] = ShareName;
        inParams["Path"] = FolderPath;
        inParams["Type"] = 0x0;
        // Finally Invoke the Create Method to do the process
        outParams = managementClass.InvokeMethod("Create", inParams, null);
        // Validation done here to check sharing is done or not
        if ((uint)(outParams.Properties["ReturnValue"].Value) != 0)
            MessageBox.Show("Folder might be already in share or unable to share the directory");
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

Cách sử dụng, các bạn gọi hàm như sau:

ShareFolderPermission(@"E:\ftp", "ftp", "Demo share folder C# - https://laptrinhvb.net");

Đầu tiên là tham số đường dẫn thư mục bạn muốn share, tiếp theo là tên share và cuối cùng là ghi chú của file share.

Kết quả khi mình chạy hàm trên:

share_folder_csharp2

CHÚC CÁC BẠN THÀNH CÔNG!

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[C#] Hướng dẫn chia sẽ share thư mục folder trong winform
Đăng bởi: Thảo Meo - Lượt xem: 5955 11:25:26, 23/04/2019C#   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp
.