第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 StreamReader 在遠程服務(wù)器上訪問和讀取文件?

如何使用 StreamReader 在遠程服務(wù)器上訪問和讀取文件?

C#
慕蓋茨4494581 2023-09-24 11:10:31
我在遠程服務(wù)器上有一個文件,我想讀取其內(nèi)容。這是我想要實現(xiàn)的一些代碼示例:StreamReader str = new StreamReader(@"\\192.168.0.1\C$\Test\test.txt"); str.ReadToEnd();這段代碼拋出:System.IO.IOException:“用戶名或密碼不正確”。我怎樣才能傳遞任何憑證?我的最終結(jié)果應(yīng)該是我正在嘗試使用以下命令訪問此 192.168.0.1user: admin和password: 123456我搜索過StreamReader文檔但沒有任何結(jié)果。
查看完整描述

2 回答

?
慕桂英546537

TA貢獻1848條經(jīng)驗 獲得超10個贊

我最終使用了以下代碼:


    public class ConnectToSharedFolder : IDisposable

    {

        readonly string _networkName;


        public ConnectToSharedFolder(string networkName, NetworkCredential credentials)

        {

            _networkName = networkName;


            var netResource = new NetResource

            {

                Scope = ResourceScope.GlobalNetwork,

                ResourceType = ResourceType.Disk,

                DisplayType = ResourceDisplaytype.Share,

                RemoteName = networkName

            };


            var userName = string.IsNullOrEmpty(credentials.Domain)

                ? credentials.UserName

                : string.Format(@"{0}\{1}", credentials.Domain, credentials.UserName);


            var result = WNetAddConnection2(

                netResource,

                credentials.Password,

                userName,

                0);


            if (result != 0)

            {

                throw new Win32Exception(result, "Error connecting to remote share");

            }

        }


        ~ConnectToSharedFolder()

        {

            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

        }

    }

}

并使用這種方式:


            using (new ConnectToSharedFolder(networkPath, credentials))

            {

                StreamReader str = new StreamReader(@"\\192.168.0.1\C$\Test\test.txt");

                var x = str.ReadToEnd();

                Console.WriteLine(x);

這段代碼正是針對我的問題而設(shè)計的!


查看完整回答
反對 回復(fù) 2023-09-24
?
臨摹微笑

TA貢獻1982條經(jīng)驗 獲得超2個贊

在訪問該文件之前,您必須進行模擬。


查看完整回答
反對 回復(fù) 2023-09-24
  • 2 回答
  • 0 關(guān)注
  • 202 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號