Visual Basic, make application only open in a specific folder - vb.net

Is there a way I can make a Visual Basic .net app open only if it's located in a specific folder/path or if it contains a certain element inside the folder?
Thanks.

you could use this to check for folder or file in application path:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'check if folder exists
If Not IO.Directory.Exists(Application.StartupPath & "\yourdirname") Then
Application.Exit()
End If
'check if file exists
If Not IO.File.Exists(Application.StartupPath & "\yourdirname\yourfile.ext") Then
Application.Exit()
End If
End Sub
End Class

Easy, On load event:
If not Application.StartupPath() = "C:\" then
Application.Exit()
End if

Related

How to overwrite a file in specific folder with a file in my program's resources

I have a file named "something.a binary extension" "not a txt extension" in my program's resources and the same file exists in another folder and named "Something.a binary extension" too.
I want to copy my resources file and replace the existing file with it.
My program has 2 button controls, one for browsing to get the path from OpenFileDialog1 and the other to copy my resources file and replace the existing file "Which have the same name" by it, and 1 TextBox to display the path.
My program has a manifest file to run as Adminstrator with level="highestAvailable.
I use this code:
Public Class Form1
Dim Path_of_folder As String
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnBrowse.Click
'getting the path
OpenFileDialog1.ValidateNames = False
OpenFileDialog1.CheckFileExists = False
OpenFileDialog1.CheckPathExists = True
OpenFileDialog1.FileName = "somthing.exe"
OpenFileDialog1.ShowDialog()
TextBox1.Text = System.IO.Path.GetFullPath(OpenFileDialog1.FileName)
TextBox1.Text = TextBox1.Text.Replace("somthing.exe", "").Trim()
Path_of_folder = TextBox1.Text 'we got the path
End Sub
Private Sub BtnCopy_Click(sender As Object, e As EventArgs) Handles BtnCopy.Click
FileCopy(My.Resources.somthing, Path_of_folder) ' this got Error when i use it, but the program can Run
End Sub
End Class

How to move files from folder to another using the paths listed in a ListBox

I have a list box that has paths to files. How can I move the selected file items that have the paths to another folder?
You can do it like this :
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For Each SelectedItem In ListBox1.SelectedItems
Try
File.Move(SelectedItem.ToString(), "New path here")
Catch : End Try
Next
End Sub
And with error handling, so if a file is not accessible it will continue to the next one.
Hope that helped you :)

How can i execute an unhide command using cmd on a drive that the users select in visual studio windows forms

I am developing an application that unhides folders and files that were hidden by a virus in windows PC's using visual studio 2012. And I'm developing it using WindowsFormsApplication. So my question is after i have received the users preferred drive using FolderBrowserDialog, how can i make my application execute an unhide command using cmd.exe like "attrib -s -h /s /d ." on the drive that the user selects?
Its not much but, here is what i'hv done so far:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If FolderBrowserDialog1.ShowDialog = DialogResult.OK Then
TextBox1.Text = FolderBrowserDialog1.SelectedPath
Label1.Text = command
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
End Sub
End Class
So when the user clicks Button_2 i want the application to execute a cmd command to unhide files and folders on the selected drive. Any idea how i can achieve that?
Am # the very beginners level, so detailed & simplified answers will be appreciated. Thanks all.
This is from MSDN
Imports System
Imports System.IO
Imports System.Text
Public Class Test
Public Shared Sub Main()
Dim path As String = "c:\temp\MyTest.txt"
' Create the file if it does not exist.
If File.Exists(path) = False Then
File.Create(path)
End If
Dim attributes As FileAttributes
attributes = File.GetAttributes(path)
If (attributes And FileAttributes.Hidden) = FileAttributes.Hidden Then
' Show the file.
attributes = RemoveAttribute(attributes, FileAttributes.Hidden)
File.SetAttributes(path, attributes)
Console.WriteLine("The {0} file is no longer hidden.", path)
Else
' Hide the file.
File.SetAttributes(path, File.GetAttributes(path) Or FileAttributes.Hidden)
Console.WriteLine("The {0} file is now hidden.", path)
End If
End Sub
Public Shared Function RemoveAttribute(ByVal attributes As FileAttributes, ByVal attributesToRemove As FileAttributes) As FileAttributes
Return attributes And (Not attributesToRemove)
End Function
End Class
You have to change it to do what you want, and do search all the folders and subfolders but it should be a good start.

Is there a way to get the input from a Load event handler (entered via inputBox) to be used Globally in the program?

I am creating a form that will prompt the user to enter a file name upon loading the file. The issue I encounter is that my variable I use to store the input is not recognized in another one of my procedures. The code here shows my current set up.
Imports System.IO
Public Class frmEmployee
Sub frmEmployee_load(ByVal sender As Object, e As System.EventArgs)
Dim strFileName = InputBox("Please name the file you would like to save the data to: ")
End Sub
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
txtEmail.Clear()
txtExtension.Clear()
txtFirst.Clear()
txtLast.Clear()
txtMiddle.Clear()
txtNumber.Clear()
txtPhone.Clear()
End Sub
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
Dim inputFile As New StreamWriter(strFileName)
If File.Exists(strFileName) = True Then
inputFile = File.CreateText(strFileName)
inputFile.Write(txtEmail.Text)
inputFile.Write(txtExtension.Text)
inputFile.Write(txtFirst.Text)
inputFile.Write(txtLast.Text)
inputFile.Write(txtMiddle.Text)
inputFile.Write(txtNumber.Text)
inputFile.Write(txtPhone.Text)
inputFile.Write(cmbDepart.Text)
Else
MessageBox.Show("" & strFileName & "Cannot be created or found.")
End If
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Me.Close()
End Sub
End Class
The load event handler is where I want the user to input the name of the file.
Change the scope of your variable to outside of your load method...
Public Class frmEmployee
Private strFileName As String
Sub frmEmployee_load(ByVal sender As Object, e As System.EventArgs)
strFileName = InputBox("Please name the file you would like to save the data to: ")
End Sub
You could use My.Settings and load the file string there and save it. All forms can now access that. When you close the the app set it to String.Empty if you want it to be.
You also could be taking advantage of a class object for the fields you are capturing then using either BinaryFormatter or XmlSerializer to store the data. Better databinding, creation and reconstruction using an object.
My.Settings.Filename = Inputbox("Filename?")
My.Settings.Save()
The problem with the approach you have there is that your variabl strFileName is scoped to the class frmEmployee.
You need to set up a genuinely global variable (at the application startup) an then use that. So you will need a Sub Main as an entry point to your application See here, and just before that create a public variable to hole the file name.
So you might have something like this for your application startup:
Public fileNametoUse as string
Public Sub Main()
Application.EnableVisualStyles()
Application.SetCompatibleTextRenderingDefault(False)
Application.Run(New Form1)
End Sub
Then in you load sub for frmEmployee you would have:
fileNametoUse = InputBox("Please name the file you would like to save the data to: ")

VB 2010 mkdir read only

Hi i cant seem to get mkdir to create a folder which isnt read only, this is causing alot of problems in my code because i am unable to write files to the directory i have created. thanks for any help. this is my code below:
Else
MessageBox.Show("Please set a Root Path for your ****")
RootFBD.ShowDialog()
TextBox1.Text = RootFBD.SelectedPath
My.Computer.FileSystem.CreateDirectory("C:\****-Tools\config\root.txt")
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim f2 As New FileIOPermission(FileIOPermissionAccess.Read, TextBox1.Text)
f2.AddPathList(FileIOPermissionAccess.Write Or FileIOPermissionAccess.Read, TextBox1.Text)
Dim rootSave As System.IO.StreamWriter
rootSave = My.Computer.FileSystem.OpenTextFileWriter("C:\****-Tools\config\root.txt", True)
rootSave.WriteLine(TextBox1.Text)
Me.Hide()
MainTool.Show()
End Sub
End Class
Thanks again josh
You're misunderstanding the problem; this isn't a permission issue.
Rather, you're leaving the file open, which prevents other processes from writing to ir.
You just need to Close() your StreamWriter.
Or, you can just call File.AppendText, which will avoid the issue.
You are creating the directory with the file name. Try this:
My.Computer.FileSystem.CreateDirectory("C:\****-Tools\config")