vb.net共享硬盘,win硬盘共享
如何把VB.NET内部资源文件复制到磁盘上
可以查看附件,或则使用以下代码。应该注释的满全了。
我们提供的服务有:成都网站设计、成都网站制作、外贸网站建设、微信公众号开发、网站优化、网站认证、爱民ssl等。为上千企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的爱民网站制作公司
随便建一个窗口工程,窗口代码帖入以下代码。
Imports System.IO
Imports System.Text
Imports System.Reflection
Public Class Form1
Const ResName As String = "123.txt" ' 需要保存的资源名
Const BufferSize As Integer = 4096 ' 复制时缓冲的大小
Private Sub Demo()
' 变量声明
Dim strDisplayText As StringBuilder ' 保存输出信息
Dim strResName As String ' 保存目标资源的名称
' 变量初始化
strDisplayText = New StringBuilder()
strResName = String.Empty
' 获取所有资源
strDisplayText.AppendLine("文件中存在以下资源:")
For Each strName As String In Assembly.GetExecutingAssembly.GetManifestResourceNames()
strDisplayText.AppendLine(" " strName)
' 获得目标资源的全名
If strName.EndsWith("." ResName) Then
strResName = strName
End If
Next
' 目标资源不存在
strDisplayText.AppendLine()
If strResName = String.Empty Then
strDisplayText.AppendLine("抱歉,没有找名为""" ResName """的资源")
Else ' 复制资源的代码
' 打开资源文件,在End Using的时候自动关闭。
Using ms As UnmanagedMemoryStream = Assembly.GetExecutingAssembly.GetManifestResourceStream(strResName)
' 打开磁盘文件,在End Using的时候自动关闭。
Using fs As New FileStream("C:\" ResName, FileMode.Create)
' 变量声明
Dim Buffer(BufferSize - 1) As Byte ' 复制文件时的缓冲数组
Dim ReadCount As Integer ' 此次读取数据的数量
' 还有数据可以读取的情况,一直循环
ReadCount = ms.Read(Buffer, 0, BufferSize)
While ReadCount 0
fs.Write(Buffer, 0, ReadCount) ' 写入到磁盘文件
ReadCount = ms.Read(Buffer, 0, BufferSize)
End While
End Using
End Using
strDisplayText.AppendLine("文件已经复制完毕。")
End If
' 显示消息
MessageBox.Show(strDisplayText.ToString())
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Call Demo()
End
End Sub
End Class
如果不用net use的方法,vb.net 有办法访问共享文件或者共享文件夹吗
我不会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中实现文件共享
调用
net
share命令即可
在vb中
可用
shell
语句
,如
shell
"net
share
XXXXXX"
NET
SHARE
sharename
sharename=drive:path
[/USERS:number
|
/UNLIMITED]
[/REMARK:"text"]
[/CACHE:Manual
|
Documents|
Programs
|
None
]
sharename
[/USERS:number
|
/UNLIMITED]
[/REMARK:"text"]
[/CACHE:Manual
|
Documents
|
Programs
|
None]
{sharename
|
devicename
|
drive:path}
/DELETE
NET
SHARE
使网络用户可以使用某一服务器上的资源。
当不带选项使用本命令
时,它会列出该计算机上正在被共享的所有资源。对于每一种资源,Windows
都
会报告其设备名或路径名,以及与之相关的描述性注释。
sharename
指共享资源的网络名。输入
NET
SHARE
和一个共享名只会
显示那个共享的有关信息。
drive:path
指定将被共享的目录的绝对路径。
/USERS:number
设置可以同时访问共享资源的最大用户数。
/UNLIMITED
指定用户可以同时访问共享资源的不受限制的数目。
/REMARK:"text"
添加一个有关资源的描述性注释,应将文本包含在引号中。
devicename
指一个或多个被共享名所共享的打印机
(LPT1:
至
LPT9:)。
/DELETE
终止资源的共享。
/CACHE:Automatic
启用脱机客户缓存和自动重新集成
/CACHE:Manual
启用脱机客户缓存和手动重新集成
/CACHE:No
告诉客户脱机缓存不合适
NET
HELP
command
|
MORE
用于逐屏显示帮助。
网站名称:vb.net共享硬盘,win硬盘共享
本文URL:http://myzitong.com/article/hcpheg.html