<<< UI >>>
<<< Source >>>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp13
{
public partial class connectcheck : Form
{
public connectcheck()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string ip = textBox1.Text.Trim();
string id = textBox2.Text.Trim();
string pw = textBox3.Text.Trim();
ConnectNetworkDrive(ip, id, pw);
}
public void ConnectNetworkDrive(string p_ip,string p_id, string p_pw)
{
string ip = $@"\\{p_ip}"; //네트워크 드라이브 주소
string userID = p_id; // 사용자 계정
string pwd = p_pw; // 비밀번호
//공유폴더 접근 객체 생성
NetworkConnector netc = new NetworkConnector();
int n = 0;
while (true)
{
n++;
int state = netc.TryConnectNetwork(ip, userID, pwd);
if (state == 0)
{
MessageBox.Show("접속됨(o)");
break;
}
if(n>50)
{
MessageBox.Show("접속불가(x)");
break;
}
}
}
}
public class NetworkConnector
{
public NETRESOURCE NetResource = new NETRESOURCE();
[DllImport("mpr.dll", CharSet = CharSet.Auto)]
public static extern int WNetUseConnection(
IntPtr hwndOwner,
[MarshalAs(UnmanagedType.Struct)] ref NETRESOURCE lpNetResource,
string lpPassword,
string lpUserID,
uint dwFlags,
StringBuilder lpAccessName,
ref int lpBufferSize,
out uint lpResult);
[DllImport("mpr.dll", EntryPoint = "WNetCancelConnection2", CharSet = CharSet.Auto)]
public static extern int WNetCancelConnection2A(String lpName, int dwFlags, int fForce);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct NETRESOURCE
{
public uint dwScope;
public uint dwType;
public uint dwDisplayType;
public uint dwUsage;
public string lpLocalName;
public string lpRemoteName;
public string lpComment;
public string lpProvider;
}
public int TryConnectNetwork(string remotePath, string userID, string pwd)
{
int capacity = 1028;
uint resultFlags = 0;
uint flags = 0;
StringBuilder sb = new StringBuilder(capacity);
NetResource.dwType = 1; // 공유 디스크
NetResource.lpLocalName = null; // 로컬 드라이브 지정하지 않음
NetResource.lpRemoteName = remotePath;
NetResource.lpProvider = null;
int result = WNetUseConnection(IntPtr.Zero, ref NetResource, pwd, userID, flags, sb, ref capacity, out resultFlags);
return result;
}
public void DisconnectNetwork()
{
WNetCancelConnection2A(NetResource.lpRemoteName, 1, 0);
}
}
}
'[ Program ] > C#' 카테고리의 다른 글
[c#] 현재값과 가장 작으면서 근접하는 5의 배수 가져오기 (1) | 2022.11.11 |
---|---|
[c#] Nlog 사용법 (0) | 2022.09.23 |
반복문도는동안 컨트롤텍스트 바꾸기(현재까지 실행된내용 라벨에 업데이트) (0) | 2015.07.22 |
c# 크로스쓰레딩 - 다른쓰레드의 컨트롤 값변경 (0) | 2015.07.22 |
C#에서 주소창 url 가져오기 (0) | 2015.03.13 |
댓글