public GetResponse ( ) : System.Net.WebResponse | ||
Результат | System.Net.WebResponse |
/// <summary> /// 下载局域网文件 /// </summary> /// <param name="path">局域网文件路径,如:\\192.168.10.1\app\app\123.zip</param> /// <param name="username">计算机名称</param> /// <param name="password">计算机密码</param> static void RequestWindowsShared(string path, string username, string password) { System.Net.FileWebRequest request = null; System.Net.FileWebResponse response = null; System.IO.Stream stream = null; System.IO.FileStream fileStream = null; try { Uri uri = new Uri(path); request = (System.Net.FileWebRequest)System.Net.FileWebRequest.Create(uri); System.Net.ICredentials ic = new System.Net.NetworkCredential(username, password); request.Credentials = ic; response = (System.Net.FileWebResponse)request.GetResponse(); stream = response.GetResponseStream(); byte[] bytes = new byte[stream.Length]; stream.Read(bytes, 0, bytes.Length); string filename = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\" + System.IO.Path.GetFileName(path); fileStream = new FileStream(filename, System.IO.FileMode.Create, System.IO.FileAccess.Write, System.IO.FileShare.ReadWrite); fileStream.Write(bytes, 0, bytes.Length); fileStream.Flush(); Console.WriteLine("下载成功!"); } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { fileStream.Close(); fileStream.Dispose(); stream.Close(); stream.Dispose(); } }