I'm making a VB application and I want to copy a file called "L3nEncrypt.jar" which is in resources of the application to the temp folder.
Right now I have this:
My.Computer.FileSystem.SpecialDirectories.Temp
which returns the temp folder.
I found the solution
Imports System.IO
File.WriteAllBytes(My.Computer.FileSystem.SpecialDirectories.Temp & "/L3nEncrypt.jar", My.Resources.L3nEncrypt)
Use System.IO.File.Copy(SourceFileName,DestinationFileName)
The parameters are full file names of type string. Make sure that you're running the application with admin privileges so that the copying can be successful.
Related
Currently, I am working on a feature that will make the files inside the folder that will not hide the file extensions using this code.
Imports Microsoft.Win32
Sub SetNoDrives(value As Integer, path as string)
Dim RegPath As String = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced]"
Using Key As RegistryKey = Registry.LocalMachine.OpenSubKey(RegPath)
Key.SetValue("HideFileExt", value, RegistryValueKind.DWord)
End Using
End Sub
the problem is, I don't know where to place the string path (folder path) on the code. the path is the specific location where you will always show the file extensions of the files inside the folder.
Any help will be much appreciated.
The reason you don't know where to put the folder path is because there is nowhere to put the folder path. This is a user-wide option, i.e. either extensions are displayed in every folder or no folder for the current user. You don't get to choose on a folder by folder basis. At least, there is no option in Windows/File Explorer to do that and I've never seen mention of it being possible, even when specifically searching for it.
In vs2010, there is a vb.net project, I need to clean a folder before use it,
what I did is remote the folder, then create it again.
but the problem is ,it is removed but not created again.
I think maybe .net did some optimization. So how can I fix that?
Here is the code:
' delete folder
If IO.Directory.Exists(exportBaseFolder) = True Then
IO.Directory.Delete(exportBaseFolder, True)
End If
' create folder
IO.Directory.CreateDirectory(exportBaseFolder)
You could try Directory.GetFiles and foreach file, file.delete. Then delete the folder. Then if not folder exists, create it.
I am new to VB. Can anyone help on this, thanks. BTW, it's bloody pain to mess the formatting here :) How to do the indent?
parameters:
source file
target
backup folder
Pseudocode:
get all sub folders under target
for each folder under target
if exists source file
if exists backup folder
replace/copy source file to backup folder
else
create backup folder
replace/copy source file to backup folder
replace source file
Check this methods:
Directory.GetDirectories()
File.Exists()
Directory.Exists()
Directory.CreateDirectory()
File.Copy()
As I told you before on the other posts, just check the methods on System.IO. Its very well documented on MSDN, specially the classes File, Directory and Path.
For getting subfolders refer to this question
You then will want to put the sub folder locations into an array. From this array, loop through each object and so on.
Try using methods under 'File.' as this will include many functions for file processing.
This will require the 'Imports System.IO'.
I want my application to look for a file in drive C's specific folder (say C:\myFolder\abc.mdb), if found just give a message if not, make the folder in drive C:\ and then copy the file.
How to do this?
Thanks
Furqan
You could use the File, Directory, and Path objects in the System.IO as shown below:
Imports System.IO
...
Dim path As String = "C:\myFolder\abc.mdb"
If File.Exists(path) Then
'TODO write code to create message'
Else
Dim folder As String = Path.GetDirectoryName(path)
If Not Directory.Exists(folder) then
Directory.CreateDirectory(folder)
End If
'TODO code to copy file from current location to the newly created directory path'
'i.e. File.Copy(FileToCopy, NewCopy)'
End If
I know this post is kind of old. I just wanted to update.
The quickest shortcut that will also create the Destination directory structure and in one line. Use Computer.FileSystem.CopyFile instead of System.IO.
My.Computer.FileSystem.CopyFile(sSourcefile, sDestinationfile)
In general,
Using VBA, how do I determine where the Current users Application Data folder is?
The FileSystemObjects special folders only knows about 3 folders
WindowsFolder
SystemFolder
TemporaryFolder
Specifically, I need a Word Macro to copy a file to the a folder under the Application Data folder.
e.g. In VB.Net I can use My.Computer.FileSystem.SpecialDirectories.CurrentUserApplicationData to do this
You can use Environ("AppData") to get this path. Environ will pull any system variable, which can be found by using the set command at the DOS prompt.
Using advapi32.dll, you can get the USERPROFILE via
Environ("USERPROFILE")
Connect this with the "Application Data" directory (which has a standard, specific name) to get what you want
CStr(Environ("USERPROFILE") & "\Application Data")
For more information, check out MSDN