重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
File.WriteAllText("\\10.1.37.222\shared\temp.txt", "");
成都创新互联公司从2013年成立,是专业互联网技术服务公司,拥有项目成都网站制作、成都做网站网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元北关做网站,已为上家服务,为北关各地企业和个人服务,联系电话:13518219792
如果能生成就没问题。
在局域网中的某台电脑(最好选择性能较好的机子)上建立一个共享文件夹(要把权限设置为所有人都可读可写),然后把文件复制到这个文件夹,只要同一局域网的其他电脑都能够在桌面的“网络”(XP之前的系统是叫“网上邻居”)中看到这个文件,并且能够进行读取、修改、删除等操作,那么在VB软件中也同样能够做到这些操作,比如打开文件可以这样:
Open
"\\电脑名称或IP地址\共享文件夹名\要操作的文件名.扩展名"
For
Input
As
#1
从中我们可以看到,操作网络共享文件与操作本地文件几乎是没有什么区别的,事实上如果把共享文件夹映射到本地服务器的虚拟驱动器上,比如把
"\\电脑名称或IP地址\共享文件夹名"
映射为驱动器z,那么上述语句就变为:
Open
"z:\要操作的文件名.扩展名"
For
Input
As
#1
这样就跟操作本地文件完全一样了!
PS:请尽量把局域网内的电脑都设为同一个家庭组或工作组,这样可以省去许多连接上的麻烦
网络共享文件夹不能直接用VB里的Open语句,那是为本地文件用的
网络文件夹用API函数OpenFile
具体用法:
FileHandle = OpenFile( "\\ " IP地址 文件路径 "\ " 文件名, lpReOpenBuff, OF_READ)
我不会VB,但是都是.net。c#的代码稍微转换一下就可以了。
C#实现访问网络共享文件夹,使用 WNetAddConnection2A 和 WNetCancelConnection2A。
在目标服务器建立共享文件夹,建立访问账号test;
public enum ERROR_ID
{
ERROR_SUCCESS = 0, // Success
ERROR_BUSY = 170,
ERROR_MORE_DATA = 234,
ERROR_NO_BROWSER_SERVERS_FOUND = 6118,
ERROR_INVALID_LEVEL = 124,
ERROR_ACCESS_DENIED = 5,
ERROR_INVALID_PASSWORD = 86,
ERROR_INVALID_PARAMETER = 87,
ERROR_BAD_DEV_TYPE = 66,
ERROR_NOT_ENOUGH_MEMORY = 8,
ERROR_NETWORK_BUSY = 54,
ERROR_BAD_NETPATH = 53,
ERROR_NO_NETWORK = 1222,
ERROR_INVALID_HANDLE_STATE = 1609,
ERROR_EXTENDED_ERROR = 1208,
ERROR_DEVICE_ALREADY_REMEMBERED = 1202,
ERROR_NO_NET_OR_BAD_PATH = 1203
}
public enum RESOURCE_SCOPE
{
RESOURCE_CONNECTED = 1,
RESOURCE_GLOBALNET = 2,
RESOURCE_REMEMBERED = 3,
RESOURCE_RECENT = 4,
RESOURCE_CONTEXT = 5
}
public enum RESOURCE_TYPE
{
RESOURCETYPE_ANY = 0,
RESOURCETYPE_DISK = 1,
RESOURCETYPE_PRINT = 2,
RESOURCETYPE_RESERVED = 8,
}
public enum RESOURCE_USAGE
{
RESOURCEUSAGE_CONNECTABLE = 1,
RESOURCEUSAGE_CONTAINER = 2,
RESOURCEUSAGE_NOLOCALDEVICE = 4,
RESOURCEUSAGE_SIBLING = 8,
RESOURCEUSAGE_ATTACHED = 16,
RESOURCEUSAGE_ALL = (RESOURCEUSAGE_CONNECTABLE | RESOURCEUSAGE_CONTAINER | RESOURCEUSAGE_ATTACHED),
}
public enum RESOURCE_DISPLAYTYPE
{
RESOURCEDISPLAYTYPE_GENERIC = 0,
RESOURCEDISPLAYTYPE_DOMAIN = 1,
RESOURCEDISPLAYTYPE_SERVER = 2,
RESOURCEDISPLAYTYPE_SHARE = 3,
RESOURCEDISPLAYTYPE_FILE = 4,
RESOURCEDISPLAYTYPE_GROUP = 5,
RESOURCEDISPLAYTYPE_NETWORK = 6,
RESOURCEDISPLAYTYPE_ROOT = 7,
RESOURCEDISPLAYTYPE_SHAREADMIN = 8,
RESOURCEDISPLAYTYPE_DIRECTORY = 9,
RESOURCEDISPLAYTYPE_TREE = 10,
RESOURCEDISPLAYTYPE_NDSCONTAINER = 11
}
[StructLayout(LayoutKind.Sequential)]
public struct NETRESOURCE
{
public RESOURCE_SCOPE dwScope;
public RESOURCE_TYPE dwType;
public RESOURCE_DISPLAYTYPE dwDisplayType;
public RESOURCE_USAGE dwUsage;
[MarshalAs(UnmanagedType.LPStr)]
public string lpLocalName;
[MarshalAs(UnmanagedType.LPStr)]
public string lpRemoteName;
[MarshalAs(UnmanagedType.LPStr)]
public string lpComment;
[MarshalAs(UnmanagedType.LPStr)]
public string lpProvider;
}
public class NetworkConnection
{
[DllImport("mpr.dll")]
public static extern int WNetAddConnection2A (NETRESOURCE[] lpNetResource, string lpPassword, string lpUserName, int dwFlags);
[DllImport("mpr.dll")]
public static extern int WNetCancelConnection2A (string sharename, int dwFlags, int fForce);
public static int Connect (string remotePath, string localPath, string username, string password)
{
NETRESOURCE[] share_driver = new NETRESOURCE[1];
share_driver[0].dwScope = RESOURCE_SCOPE.RESOURCE_GLOBALNET;
share_driver[0].dwType = RESOURCE_TYPE.RESOURCETYPE_DISK;
share_driver[0].dwDisplayType = RESOURCE_DISPLAYTYPE.RESOURCEDISPLAYTYPE_SHARE;
share_driver[0].dwUsage = RESOURCE_USAGE.RESOURCEUSAGE_CONNECTABLE;
share_driver[0].lpLocalName = localPath;
share_driver[0].lpRemoteName = remotePath;
Disconnect(localPath);
int ret = WNetAddConnection2A(share_driver, password, username, 1);
return ret;
}
public static int Disconnect (string localpath)
{
return WNetCancelConnection2A(localpath, 1, 1);
}
调用方式:
public static void TestNetWorkConnection()
{
string localpath = "X:";
//int status = NetworkConnection.Connect(@"\\192.168.0.2\test", localpath, @"test", "test");
int status = NetworkConnection.Connect("\\\\192.168.0.2\\test", localpath, @"test", "test");
if (status == (int)ERROR_ID.ERROR_SUCCESS)
{
FileStream fs = new FileStream(localpath + @"\\123.txt", FileMode.OpenOrCreate);
using (StreamWriter stream = new StreamWriter(fs))
{
stream.WriteLine("你好呀,成功了");
stream.Flush();
stream.Close();
}
fs.Close();
}
else
{
Console.WriteLine(status);
}
NetworkConnection.Disconnect(localpath);
}
VB访问网络共享文件夹与访问本地文件夹是没有什么区别的,比如你要打开与你的电脑同一个局域网的另一台名为“abcd”的电脑的共享文件夹“xxx”中的文本文件“123.txt”,可以这样:
Open "\\abcd\xxx\123.txt" For Input As #1
也可以用IP地址,比如:
Open "\\192.168.1.103\xxx\123.txt" For Input As #1