How to rename directory with WinSCP .NET assembly? - vb.net

I want to be able to rename directory whether it contains something or not. I do not see such option. For instance this is how I create directory, but how to rename it?:
Public Sub CreateDirectory(path As String)
If session IsNot Nothing Then
session.CreateDirectory(path)
End If
End Sub

Use the Session.MoveFile method:
session.MoveFile("/path/directory", "/path/new_name")

Related

How to double check if folder is empty`?

I can't find a solution for my problem. My Code is deleting empty folders and in general working fine, but there is one exception. It goes through every path one time, but if there was a Folder (A) that only has empty Folder (B) in it, then it would only delete Folder(B), since Folder (A) was not at empty at the time. How can I make it, so that it understands that Folder (A) is gonna be empty, once Folder (B) is deleted?
I thought DeleteEmptyFolder(folder.FullName) would solve the problem, but it is not working, since it doesn't repeat the same path it already went through. Removing directory.GetDirectories.Count = 0 doesn't work either, since it would delete any folder that doesn't have a file in it (even if there is another folder with files in it)
Private Sub DeleteEmptyFolder(ByVal sDirectoryPath As String)
If IO.Directory.Exists(sDirectoryPath) Then
Dim directory As New IO.DirectoryInfo(sDirectoryPath)
If directory.GetDirectories.Count = 0 AndAlso directory.GetFiles.Count = 0 Then
directory.Delete(True)
Return
End If
For Each folder As IO.DirectoryInfo In directory.GetDirectories()
DeleteEmptyFolder(folder.FullName)
Next
End If
End Sub
I am fairly new to VB.Net, so pardon if it is an obvious answer that I don't see.
Here's how the code ought to look:
Private Sub DeleteEmptyFolder(folderPath As String)
If Directory.Exists(folderPath) Then
For Each subFolderPath In Directory.EnumerateDirectories(folderPath)
DeleteEmptyFolder(subFolderPath)
Next
If Directory.EnumerateFiles(folderPath).Any() OrElse
Directory.EnumerateDirectories(folderPath).Any() Then
Return
End If
Directory.Delete(folderPath)
End If
End Sub
There's no point using DirectoryInfo if you need no other information about files and folders other than path. You should use EnumerateFiles and EnumerateDirectories over GetFiles and GetDirectories unless you specifically need to get an array of entries up front. In this case, you definitely don't. Let's say that you had a folder with 1000 files in it. This:
directory.GetFiles.Count = 0
would create an array containing an element for all 1000 files first, then check the number of elements in it. On the other hand, this:
Directory.EnumerateFiles(folderPath).Any()
would return True as soon as it encountered the first file, ignoring the other 999. You only care whether there's any files in the folder, not how many there are.
Please try this:
Private Sub deleteEmptyFolders(ByRef folder As String)
'Does exist such a path?
If IO.Directory.Exists(folder) Then 'yes
'Loop over all directories
For Each subFolder In IO.Directory.GetDirectories(folder)
'Delete all empty folders
deleteEmptyFolders(subFolder)
Next
'Delete folder if nothing remained in it
Try
My.Computer.FileSystem.DeleteDirectory(folder, FileIO.DeleteDirectoryOption.ThrowIfDirectoryNonEmpty)
Catch ex As Exception
End Try
End If
End Sub
I think this does what you want in a simple way.
I solved the problem by moving some of the code around.
Private Sub DeleteEmptyFolder(ByVal sDirectoryPath As String)
If IO.Directory.Exists(sDirectoryPath) Then
Dim directory As New IO.DirectoryInfo(sDirectoryPath)
For Each folder As IO.DirectoryInfo In directory.GetDirectories()
DeleteEmptyFolder(folder.FullName)
Next
If directory.GetDirectories.Count = 0 AndAlso directory.GetFiles.Count = 0 Then
directory.Delete(True)
Return
End If
End If
End If

VB .NET How to copy a directory without its subfolders

I want t copy all files in a directory, but ignore all sub-folders. Is there a single function to do this?
The standard function:
My.Computer.FileSystem.CopyDirectory(inputDir.FullName, outputDir.FullName)
copies all the sub-folders.
Iterating over all files:
For Each file In inputDir.GetFiles()
file.CopyTo(Path.Combine(outputDir.FullName, file.Name), True)
Next
seems to work fine. But it looks too complex to me. Is there a simpler way?
Make it into a function ...
Public Sub CopyFiles(inputPath As System.IO.DirectoryInfo,
outputPath As System.IO.DirectoryInfo)
For Each fi In inputPath.GetFiles()
fi.CopyTo(Path.Combine(outputPath.FullName, fi.Name), True)
Next
End Sub
... so it can simply be called like this when you want to use it
CopyFiles(New DirectoryInfo("C:\test1"), New DirectoryInfo("C:\test2"))

Vb.net interface with power point

Hi i tried the following simple code, but it has error of
Object variable or With block variable not set.
Module Module1
Sub main()
Dim ppt As Presentation = Nothing
ppt.LoadFromFile("C:\Users\310238479\Desktop\test.pptx")
End Module
End Sub
I seems that you are not using vb.net only, but spire.presentation.
As the commands are correct, please verify, that you try to access an existing file and you have sufficient rights ...

Configure App.config Application Settings During MSI Install vb.net

I am trying to create an msi install for my windows service. The reason for creating an msi is that the intended users want to be able to quickly install the service with as-little intervention as possible.
i can get the service to install as a msi but i have a variable within my code that i need the user to define when the msi is being installed. the variable i require from the user is the file path which they want the xml files my service creates to be located.
i thought i could configure the app.config application settings to contain the file path that the xml files should be written to. However i'm struggling to do this and im not to sure if its the best way to do it?
I have my setup project that contains my executable and has my textbox which will contain the one variable from the user.
I have an installer class which contains my serviceinstaller and process installer. This is where im struggling to understand what i need to do next.
Do i need to override the install method? The current code of my installer class was automatically generated and is as follows:
Imports System.Configuration.Install
Imports System.Configuration
<System.ComponentModel.RunInstaller(True)> Partial Class ProjectInstaller
Inherits System.Configuration.Install.Installer
'Installer overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
'Required by the Component Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Component Designer
'It can be modified using the Component Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.ServiceProcessInstaller1 = New System.ServiceProcess.ServiceProcessInstaller()
Me.ServiceInstaller1 = New System.ServiceProcess.ServiceInstaller()
'
'ServiceProcessInstaller1
'
Me.ServiceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem
Me.ServiceProcessInstaller1.Password = Nothing
Me.ServiceProcessInstaller1.Username = Nothing
'
'ServiceInstaller1
'
Me.ServiceInstaller1.ServiceName = "Spotter"
Me.ServiceInstaller1.StartType = System.ServiceProcess.ServiceStartMode.Automatic
'
'ProjectInstaller
'
Me.Installers.AddRange(New System.Configuration.Install.Installer() {Me.ServiceProcessInstaller1, Me.ServiceInstaller1})
End Sub
Friend WithEvents ServiceProcessInstaller1 As System.ServiceProcess.ServiceProcessInstaller
Friend WithEvents ServiceInstaller1 As System.ServiceProcess.ServiceInstaller
End Class
I can even add the CustomActionData values. The string determines what gets passed into the context object that i used to collect the user value that is entered. param1 being my variable name.
I'm pretty much struggling with the installer code...i think?
One option to consider is using a third party installer, such as Installshield, that has builtin support for modification of xml configuration files, such as config files.
However, if you want to roll your own, you definitely need to override the Install method. Any parameters that you pass in CustomData will be available in the dictionary that is passed as a parameter to this method.
For example:
Public Overrides Sub Install(ByVal stateSaver As System.Collections.IDictionary)
MyBase.Install(stateSaver)
If Me.Context.Parameters.Count <> 0 Then
For Each sKey As String In Context.Parameters.Keys
Select Case sKey.ToUpper
Case "PARAM1"
' XML directory
Me.XMLDir = Context.Parameters(sKey)
End Select
Next
End If
End Sub
In cases like this, we always write the value to the registry so that the user doesn't have to re-enter it if they uninstall or reinstall.
I am not sure of the exact sequence of events for modifying the app.config, but you could write to the registry, then modify app.config when the service is first started.
You will probably also find that you need to remove the custom action from the Commit phase in order for your installer to work successfully.

Open multiple files using arguments

I'm using this code to load multiple files using windows context menu, but the problem is that the aplication is open many times as files the user has selected.
For example: If I select 14 files, an open them with the application, the aplicacion is opened 14 times and load the form only one.
But there is a way to send all arguments once? Because %1 send only one file, or there is for example a %2 that send all file pats in one argument? If there is I'vent found.
This my actual code:
Public Class Program
Public Shared Sub Main()
Dim FurBase As New Core.clsDatabase
FurBase.Directory = My.Application.Info.DirectoryPath
Dim returnValue As String()
returnValue = Environment.GetCommandLineArgs()
If returnValue.Length > 1 Then
FurBase.AddTemporalFilepath(returnValue(1).ToString)
End If
If Not Process.GetProcessesByName(Process.GetCurrentProcess.ProcessName).Length > 1 Then
ShowUploader()
End If
End Sub
Private Shared Sub ShowUploader()
Dim Uploader As New frmUploader
Application.EnableVisualStyles()
Application.Run(Uploader)
End Sub
End Class
Please tell me what think about the code and if ther is any way to improve it.
Regards~
I was reading about that today; seems you'll need to deal with a DDE server.
There are an old question which can help you: What is the best .net alternative to dde for file associations?