C#訪問本地網絡目錄及文件

來自你的消息:

怎麼通過C#訪問局域網目錄?用戶名和密碼怎麼設置?

來自ChatAI的消息:

要使用C#訪問局域網目錄,可以使用.NET Framework中的System.Net命名空間提供的類和方法來實現。你可以使用NetworkCredential類設置用戶名和密碼進行身份驗證。下面是一個簡單的示例代碼:

using System;
using System.Net;

class Program
{
    static void Main(string[] args)
    {
        string remoteUri = "file://192.168.1.100/shared_folder/file.txt";
        string fileName = "file.txt";
        string userName = "yourUsername";
        string password = "yourPassword";

        // 創建一個WebClient實例,並設置用戶名和密碼
        WebClient client = new WebClient();
        client.Credentials = new NetworkCredential(userName, password);

        try
        {
            // 下載文件
            client.DownloadFile(remoteUri, fileName);
            Console.WriteLine("文件下載成功!");
        }
        catch (Exception ex)
        {
            Console.WriteLine("發生錯誤: " + ex.Message);
        }
    }
}

請將remoteUri替換爲你要訪問的目錄或文件的路徑,將userNamepassword替換爲正確的用戶名和密碼。這樣,你就可以使用指定的用戶名和密碼訪問局域網目錄。

來自你的消息:

C#怎麼通過用戶名密碼訪問網絡文件夾和文件?

來自ChatAI的消息:

在C#中,你可以使用System.Net命名空間提供的WebClient類來通過用戶名和密碼訪問網絡文件夾和文件。以下是一個示例代碼,演示瞭如何通過用戶名和密碼訪問網絡文件夾和文件:

using System;
using System.Net;

class Program
{
    static void Main(string[] args)
    {
        // 設置目標文件夾和文件路徑
        string folderUrl = "http://example.com/files/";
        string fileUrl = "http://example.com/files/file.txt";

        // 設置需要使用的用戶名和密碼
        string userName = "your_username";
        string password = "your_password";

        // 創建一個WebClient實例,並設置用戶名和密碼
        WebClient client = new WebClient();
        client.Credentials = new NetworkCredential(userName, password);

        try
        {
            // 訪問文件夾
            string[] folderContent = client.DownloadString(folderUrl).Split(new[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
            Console.WriteLine("文件夾內容:");
            foreach (string item in folderContent)
            {
                Console.WriteLine(item);
            }

            Console.WriteLine();

            // 下載文件
            client.DownloadFile(fileUrl, "local_file.txt");
            Console.WriteLine("文件下載成功!");
        }
        catch (WebException ex)
        {
            Console.WriteLine("發生錯誤: " + ex.Message);
        }
    }
}

在示例代碼中,folderUrl是目標文件夾的URL,fileUrl是目標文件的URL。你可以將它們替換爲你要訪問的實際URL。然後,使用userNamepassword設置用戶名和密碼。使用WebClient類的DownloadString方法可以獲取文件夾的內容,使用DownloadFile方法可以下載文件。請將示例代碼中的用戶名、密碼和URL替換爲你要訪問的網絡文件夾和文件相關的憑據和URL。

來自你的消息:

C#怎麼通過用戶名密碼訪問本地網絡文件夾和文件?

來自ChatAI的消息:

在C#中,如果你要訪問本地網絡文件夾和文件,並提供用戶名和密碼進行身份驗證,可以使用System.IO命名空間提供的類和方法。以下是一個示例代碼,演示瞭如何通過用戶名和密碼訪問本地網絡文件夾和文件:

using System;
using System.IO;

class Program
{
    static void Main(string[] args)
    {
        // 設置本地網絡文件夾路徑
        string folderPath = @"\\server\share\folder\subfolder\";

        // 設置需要使用的用戶名和密碼
        string userName = "your_username";
        string password = "your_password";

        try
        {
            // 使用用戶名和密碼進行身份驗證
            using (new NetworkConnection(folderPath, new NetworkCredential(userName, password)))
            {
                // 訪問文件夾
                string[] folderContent = Directory.GetFiles(folderPath);
                Console.WriteLine("文件夾內容:");
                foreach (string file in folderContent)
                {
                    Console.WriteLine(file);
                }

                Console.WriteLine();

                // 訪問文件
                string filePath = Path.Combine(folderPath, "file.txt");
                if (File.Exists(filePath))
                {
                    Console.WriteLine("文件存在!");
                }
                else
                {
                    Console.WriteLine("文件不存在!");
                }

                Console.WriteLine();
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("發生錯誤: " + ex.Message);
        }
    }
}

public class NetworkConnection : IDisposable
{
    string _networkName;

    public NetworkConnection(string networkPath, NetworkCredential credentials)
    {
        _networkName = networkPath;

        var netResource = new NetResource
        {
            Scope = ResourceScope.GlobalNetwork,
            ResourceType = ResourceType.Disk,
            DisplayType = ResourceDisplaytype.Share,
            RemoteName = networkPath
        };

        var userName = string.IsNullOrEmpty(credentials.Domain)
            ? credentials.UserName
            : $@"{credentials.Domain}\{credentials.UserName}";

        var result = WNetAddConnection2(
            netResource,
            credentials.Password,
            userName,
            0);

        if (result != 0)
        {
            throw new Win32Exception(result);
        }
    }

    ~NetworkConnection()
    {
        Dispose(false);
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    protected virtual void Dispose(bool disposing)
    {
        WNetCancelConnection2(_networkName, 0, true);
    }

    [DllImport("mpr.dll")]
    private static extern int WNetAddConnection2(NetResource netResource,
        string password, string username, int flags);

    [DllImport("mpr.dll")]
    private static extern int WNetCancelConnection2(string name, int flags,
        bool force);

    [StructLayout(LayoutKind.Sequential)]
    public class NetResource
    {
        public ResourceScope Scope;
        public ResourceType ResourceType;
        public ResourceDisplaytype DisplayType;
        public int Usage;
        public string LocalName;
        public string RemoteName;
        public string Comment;
        public string Provider;
    }

    public enum ResourceScope : int
    {
        Connected = 1,
        GlobalNetwork,
        Remembered,
        Recent,
        Context
    };

    public enum ResourceType : int
    {
        Any = 0,
        Disk = 1,
        Print = 2,
        Reserved = 8,
    }

    public enum ResourceDisplaytype : int
    {
        Generic = 0x0,
        Domain = 0x01,
        Server = 0x02,
        Share = 0x03,
        File = 0x04,
        Group = 0x05,
        Network = 0x06,
        Root = 0x07,
        Shareadmin = 0x08,
        Directory = 0x09,
        Tree = 0x0a,
        Ndscontainer = 0x0b
    }
}

在示例代碼中,folderPath是本地網絡文件夾的路徑,你需要將其替換爲你實際要訪問的路徑。然後,使用userNamepassword設置需要使用的用戶名和密碼。通過使用NetworkConnection類進行身份驗證,它使用指定的用戶名和密碼嘗試建立到本地網絡文件夾的連接。在使用using語句創建NetworkConnection實例之後,你可以使用Directory.GetFiles方法來獲取文件夾的內容,並使用File.Exists方法檢查文件是否存在。請將示例代碼中的用戶名、密碼和文件夾路徑替換爲你要訪問的本地網絡文件夾和文件相關的

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章