level 6
VB怎么把一个文件 复制到内存,是复制到内存里!!
假设 我点了一下 VB里的某个按钮( 或者按了某个全局热键 这个我自己会实现) 就可以把程序根目录下的 a.txt复制到内存
这样的话 无论后来打开到什么目录下,都可以很快的按CTRL+V 实现粘帖这个文件 很方便!!
因为我不知道我会在什么目录下粘帖 所以我需要按下按钮后复制到内存 而不是指定的目录
百度搜索了半天 没搜索到有用的消息 还有很多其他的程序语言干扰,**死 求办法 跪求
2020年08月27日 16点08分
1
吧务
level 13
' wjm1 源文件;wjm2 目标文件;Text3、Text4 放置进度数据
' 文件复制成功——复制文件(wjm1, wjm2) 输出【True】否则【False】
Function 复制文件(wjm1 As String, wjm2 As String) As Boolean
Dim Ts As String, n As Long, i As Long
复制文件 = False
If wjm1 = wjm2 Then
Ts = "【目标文件】和【源文件】的名称相同,"
Ts = Ts & "不能进行复制操作! "
Else
Open wjm1 For Binary As #1 ' 打开源文件:WJM1
Open wjm2 For Binary As #2 ' 创建新文件:WJM2
n = Int(LOF(1))
If n < 1 Then
Ts = "【源文件】不存在,不能进行复制操作! "
Else
i = 0: Text3 = "": Text4 = ""
ProgressBar1.Min = 0 ' 初始化进度条
ProgressBar1.Max = n
Do While Not EOF(1)
Get #1, , CH ' 读入一个字节
Put #2, , CH ' 写出一个字节
Text3.Text = Text3.Text + CH
Text4.Text = Text4.Text + CH
i = i + 1
ProgressBar1.Value = i ' 进度条位置
Loop
ProgressBar1.Value = n
Ts = "目标文件【" & wjm2 & "】" & Trim(Str(n))
Ts = Ts & " 字节复制成功! "
复制文件 = True
End If
Close
End If
MsgBox Ts, 0 + 64, "系统提示"
End Function
2020年08月28日 05点08分
6
注意:【目标文件】和【源文件】都含全路径。
2020年08月28日 05点08分
@Luosen56 大佬你是捣乱的吗,我要做的只是复制一个只有不超过10个字符的txt文件进内存,哪要什么进度条,而且你连剪贴板都没操作,不是把文本里的文字复制进去,而是把文本文档txt文件复制进去
2020年08月28日 07点08分
你没有谈到【10个字节】这个概念。我写的是任意大小的文件均可复制,而且无需用到【剪贴板】。一般来说,文件长度在3MB以内可以使用VB开辟的内存来操作;文件长度比较大时,本函数即可实现拷贝功能。
2020年08月28日 11点08分
level 6
我找来了一个实例 但是这个实例是把图片放进内存的 有没有大佬改成把txt文件放进内存的
Const LR_LOADFROMFILE = &H10
Const IMAGE_BITMAP = 0
Const IMAGE_ICON = 1
Const IMAGE_CURSOR = 2
Const IMAGE_ENHMETAFILE = 3
Const CF_BITMAP = 2
Private Declare Function LoadImage Lib "user32" Alias "LoadImageA" (ByVal hInst As Long, ByVal lpsz As String, ByVal dwImageType As Long, ByVal dwDesiredWidth As Long, ByVal dwDesiredHeight As Long, ByVal dwFlags As Long) As Long
Private Declare Function CloseClipboard Lib "user32" () As Long
Private Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function EmptyClipboard Lib "user32" () As Long
Private Declare Function SetClipboardData Lib "user32" (ByVal wFormat As Long, ByVal hMem As Long) As Long
Private Declare Function IsClipboardFormatAvailable Lib "user32" (ByVal wFormat As Long) As Long
Private Sub Form_Load()
Dim hDC As Long, hBitmap As Long
'Load the bitmap into the memory
hBitmap = LoadImage(App.hInstance, "c:\windows\logow.sys", IMAGE_BITMAP, 320, 200, LR_LOADFROMFILE)
If hBitmap = 0 Then
MsgBox "There was an error while loading the bitmap"
Exit Sub
End If
'open the clipboard
OpenClipboard Me.hwnd
'Clear the clipboard
EmptyClipboard
'Put our bitmap onto the clipboard
SetClipboardData CF_BITMAP, hBitmap
'Check if there's a bitmap on the clipboard
If IsClipboardFormatAvailable(CF_BITMAP) = 0 Then
MsgBox "There was an error while pasting the bitmap to the clipboard!"
End If
'Close the clipboard
CloseClipboard
'Get the picture from the clipboard
Me.Picture = Clipboard.GetData(vbCFBitmap)
End Sub
2020年08月28日 09点08分
9
@jy497759649 感觉很难就对了,连贴吧里的老手都解决不了,我想靠自己搞定是有难度了
2020年08月28日 11点08分
TXT 文件放入内存,一般是在窗体中建一个多行文本框【Text1】,然后将其隐藏(不显示)。对于外部的 TXT 文件就可以读入这个文本框中(可达3MB左右),需要时就像外部输出出去即可。
2020年09月02日 16点09分
@Luosen56 完全是2码事,说来说去还是复制文本里面的文本内容,而不是复制文件本身,我后来加了个VB的群,里面有老手给解决了,他们还专门写入到DLL文件里了,一个函数就搞定了,在任意的目录下都可以把txt文件粘帖出来
2020年09月03日 04点09分