How to allow access to a folder? - vb.net

Trying to write text from a text box to a file, keeping any text that is already in there, but this message comes up:
An unhandled exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll
Access to the path 'C:\Tickets.txt' is denied.
This is for a school assignment to create an IT ticketing software for a school.
What I have done so far:
Private Sub BtnComSubmit_Click(sender As Object, e As EventArgs) Handles BtnComSubmit.Click
My.Computer.FileSystem.WriteAllText("C:\Tickets.txt", TxtComList.Text, True)
End Sub
How could I give the necessary permission(s) to perform this action?

Change this:
My.Computer.FileSystem.WriteAllText("C:\Tickets.txt", TxtComList.Text, True)
To this:
Dim desktopPath As String = Environment.GetFolderPath( Environment.SpecialFolder.Desktop )
Dim filePath As String = System.IO.Path.Combine( desktopPath , "Tickets.txt" )
My.Computer.FileSystem.WriteAllText( filePath , TxtComList.Text, True )
BTW, I recommend avoiding the VB-specific My.Computer... stuff and using the System.IO API so you can graduate over to C# eventually with greater familiarity with .NET's APIs. Very few people stick with VB.NET for long.

Related

VB.Net RecycleBin is not declared

I refer to this post How to retrieve the 'Deletion Date' property of an Item stored in the Recycle Bin using Windows API Code Pack?
I refer to the answer by #ElektroStudios. I am trying to run that code. My knowledge of VB.net is very little.
Imports Microsoft.WindowsAPICodePack.Shell
Imports System.Text
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim RecycledFiles As ShellFile() = RecycleBin.MasterBin.Files
Dim sb As StringBuilder
' Loop through the deleted Items.
For Each Item As ShellFile In RecycledFiles
' Append the full name
sb.AppendLine(Item.Name)
' Append the DateDeleted.
sb.AppendLine(Item.Properties.GetProperty("DateDeleted").ValueAsObject.ToString)
MsgBox(sb.ToString)
sb.Clear()
Next Item
End Sub
End Class
However, I get a compiler error that RecycleBin is not declared. at
RecycleBin.MasterBin.Files
I am not too sure how to make this work. What is it that I am missing here? Is that a correct code ? Am I missing any Imports or any references?
I have already installed
nuget\Install-Package WindowsAPICodePack-Core
nuget\Install-Package WindowsAPICodePack-Shell
Note - I have already succeeded in accessing the RecycleBin using
SH.NameSpace(Shell32.ShellSpecialFolderConstants.ssfBITBUCKET)
I am specifically interested in that above piece of code. Thanks
To get the deletion date for items in the Recycle Bin, you don't need any extra libraries, you can use the Shell Objects for Scripting and Microsoft Visual Basic library (which I understand you already found in your last sentences) and the ExtendedProperty method.
Here is some code that dumps items in the recycle bin and their deletion date:
Sub Main()
Dim shell = Activator.CreateInstance(Type.GetTypeFromProgID("Shell.Application"))
Const ssfBITBUCKET As Integer = 10
Dim folder = shell.Namespace(ssfBITBUCKET)
For Each item In folder.Items
' dump some standard properties
Console.WriteLine(item.Path)
Console.WriteLine(item.ModifyDate)
' dump extended properties (note they are typed, here as a Date)
Dim dd As Date = item.ExtendedProperty("DateDeleted")
Console.WriteLine(dd)
' same but using the "canonical name"
' see https://learn.microsoft.com/en-us/windows/win32/api/propsys/nf-propsys-psgetpropertydescriptionbyname#remarks
Console.WriteLine(item.ExtendedProperty("System.Recycle.DateDeleted"))
Next
End Sub

I'm trying to add the bookmark in word using VB.net..have anyone got idea?

Imports DocumentFormat.OpenXml
Imports DocumentFormat.OpenXml.Wordprocessing
Imports DocumentFormat.OpenXml.Packaging
Public Class Add_bookmark
Const fileName As String = "F:\vb\part2 here\AddRemove.docx"
Const bookmarkName As String = "Page1"
Private Sub Add_bookmark_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Using doc As WordprocessingDocument = WordprocessingDocument.Open(fileName, True)
Dim docBody As Body = doc.MainDocumentPart.Document.Body
Dim addBookmark As BookmarkStart = docBody.Descendants(Of BookmarkStart)().FirstOrDefault(Function(a) a.Name = bookmarkName)
If addBookmark Is Nothing Then
Throw New Exception(String.Format("Bookmark {0} not found", bookmarkName))
End If
'addBookmark.InsertAt(bookmarkName)
doc.MainDocumentPart.Document.Save()
End Using
End Sub
End Class
Here's my recommendation that I think will pretty much solve most Open XML SDK issue. They have both a comparison tool and a code generation tool. Use thoughts to your advantage.
Create the document you want to see in Microsoft Word. Save It.
Open the document again, add a bookmark. Save it again, but under a different name.
Open the XML SDK comparison tool and select both documents. It'll show you the differences, and also will show you the sample .NET code that can be used to create the 2 documents. In this case, you'll focus on the differences in the code.

Visual Basic Form Coding (how to set a directory)

Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
Dim Newline As String
Newline = System.Environment.NewLine
System.IO.File.WriteAllLines("C:\Users\Sang\Desktop\filename.txt", Result1.Lines)
System.IO.File.AppendAllLines("C:\Users\Sang\Desktop\filename.txt", Result2.Lines)
System.IO.File.AppendAllLines("C:\Users\Sang\Desktop\filename.txt", values.Lines)
End Sub
This is my coding for making a text file on my desktop. However, my friend can not run this code because this code is only for myself as you can see above. I would like to use a folderbroswerdialog to generalize this coding for everyone. To be specific, if a user pressed this button on the form, folder browser should ask him where he wants to save this text file and text file should be saved in the directed folder or desktop. I tried to do it on my own by looking at many youtube videos and resources but I failed. How should I proceed this?
You can use the Environment.SpecialFolder Enumeration which contains the ubications of the system's directories that you can retrieve using Environment.GetFolderPath method :
Dim DesktopDir As String =
Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
Then you can use the Path.Combine method to properlly combine a directory/file path:
Dim OutputFile As String =
IO.Path.Combine(DesktopDir, "filename.txt")
Then:
IO.File.WriteAllLines(OutputFile, "Text Here")
Take a look here for using the FolderBrowserDialog:
http://msdn.microsoft.com/en-us/library/system.windows.forms.folderbrowserdialog(v=vs.110).aspx
PERFECTLY great examples there.

How to close parent form and open child?

Hey guys before I was just hiding the parent form, but now when I try to read from the parent file it says it can't because it's already running in a process. I followed some tutorial and it said to go to the project properties and have the application stop running when all the forms are closed.
But now since I did that it says the directory can't be found probably because I am reading the input from the parent form. Anyways here is my code
Dim writeFile1 As StreamWriter = New StreamWriter(File.OpenWrite("C:\Users\Nick\Documents\Visual Studio 2010\Projects\LoginFixed\Accounts\" + frmLogin.txtUser.Text))
How should I go about doing this?
Edit:
Private Sub btnHunter_Click(sender As System.Object, e As System.EventArgs) Handles btnHunter.Click
selection = "Hunter"
writeData.classSelection()
End Sub
This is what I have when the button is clicked.
Here is the classSelection sub:
Public Sub classSelection()
If frmClass.selection = "Hunter" Then
writeFile1.WriteLine(frmClass.selection)
End If
If frmClass.selection = "Gatherer" Then
writeFile1.WriteLine(frmClass.selection)
End If
If frmClass.selection = "Farmer" Then
writeFile1.WriteLine(frmClass.selection)
End If
writeFile1.Close()
End Sub
The error points to this line:
If frmClass.selection = "Hunter" Then
Saying part of the file path cannot be found.
If you want to read input textbox in closed parent form, you have to declare public var
Make a new module in your project .. and add this
public sLogin as String
And before you hide or close frmLogin .. add this
sLogin = txtUser.Text
So, you could change your code with
Dim writeFile1 As StreamWriter = New StreamWriter(File.OpenWrite("C:\Users\Nick\Documents\Visual Studio 2010\Projects\LoginFixed\Accounts\" & sLogin))
matzone has given you a good hint. And to check exactly what your path is, just add a MessageBox using variables :
Dim writePath1 As String
Dim writeFile1 As StreamWriter
writePath1 = "C:\Users\Nick\Documents\Visual Studio 2010\Projects\LoginFixed\Accounts\" & sLogin
If MessageBox.Show(writePath1, "Continue ?", MessageBoxButtons.YesNo) = DialogResult.Yes Then
writeFile1 = New StreamWriter(File.OpenWrite(writePath1))
' ...
writeFile1.Close() ' Very important ! Adrian pointed it out.
End If
^^ and if it works, you can discard the Dialog test or replace it by some test code like If File.Exists(...)
However, I don't understand wether you want to close the parent Form or hide it. It's different !
Closing the parent Form will discard any access to parent Form members, including txtUser.Text.
If you want to close the parent Form, the ChildForm should not be a child of that parent you are trying to close, or you must just hide the parent Form :
frmLogin.Hide() ' Not frmLogin.Close()
If you close frmLogin, frmLogin.txtUser won't be accessible, or use sLogin provided by matzone instead. Alternatively, you should pass frmLogin.txtUser.Text value to a custom property of ChildForm.
Imports System.IO
Public Partial Class ChildForm1
' Inherits System.Windows.Form
' ...
Private _txtUserFile As String
Public WriteOnly Property TxtUserFile() As String
Set(ByVal NewFileName As String)
_txtUserFile = NewFileName
End Set
End Property
Public Sub LoadFile()
Dim writeFile1 As StreamWriter = New StreamWriter(File.OpenWrite("C:\Users\Nick\Documents\Visual Studio 2010\Projects\LoginFixed\Accounts\" & txtUserFile))
' ...
writeFile1.Close()
End sub
' ...
End Class
Then use this in parent Form :
MyChildForm.TxtUserFile = Me.txtUser.Text
' Me.Close() ' This will definately KILL Form1 (the parent one)
Me.Hide() ' Always use Hide() until you'll terminate your Application
MyChildForm.Show()
MyChildForm.LoadFile()
^^ but this is not a good code either ! Your problem remains unclear (at least for me)
"Still saying it can't find part of the path", then check the path..
Does the file actually exists ?
Does the path contains glitch ? (use the provided MessageBox test)
Does your account can access that directory ? (Windows configuration and account levels)
Well !
In fact, the problem could be somewhere else.
For example, I was able to reproduce your exception by providing an empty string [""] as the value of, either :
frmLogin.txtUser.Text ' = ""
' or
sLogin ' = ""
' or
txtUserFile ' = ""
In fact, I get the "Could not find a part of the path..." exception because the StreamWriter couldn'd read/write to a File, as I didn't provided a valid FileName for that file. As the filename parameter was an empty string "", the provided path for StreamWriter was just representing a directory instead of a file and an exception was raised.
Now, you should check wether you have a valid path before building a new instance of StreamWriter to get sure you are actually pointing to a File ?
Dim writeFile1 As StreamWriter
Dim MyEntirePath As String = "C:\Users\...\Accounts\" + frmLogin.txtUser.Text
MessageBox.Show(MyEntirePath) ' would be enough to be sure your path is correct
' Some test code here...
If everythingOK then ' create the StreamWriter...
writeFile1 = New StreamWriter(MyEntirePath)
' ...
' ...
Also, it's not a good idea to create your streamwriter, and use it in another part/method of your code. You never known if one day, you'll change your code, and forget to make the link between
Dim writeFile1 As StreamWriter = New StreamWriter(File.OpenWrite("C:\Users\Nick\Documents\Visual Studio 2010\Projects\LoginFixed\Accounts\" + frmLogin.txtUser.Text))
' plus
Private Sub btnHunter_Click(sender As System.Object, e As System.EventArgs)
...
End Sub
' plus
Public Sub classSelection()
...
writeFile1.Close()
End Sub
^^ too much "here and there"...
You'll obviously also get an exception if you try to click btnHunter twice.. I don't know what is the purpose of your code nor how it works, it looks like a game.. But I would use File.Exist(..) checks, create the file before, if none, and put that in a Try/Catch to check if I eventually don't have administrator rights to write to that directory. Otherwise, make a code that allow user to read/write files to a custom folder. Andalso, you have :
Application.StartupPath
^^ Very usefull, like :
Dim MyFilePath As String = Application.StartupPath + "\Datas\MyText.txt"
After two weeks of coding, I usually forget where I put those "C:\blabla.." or "D:\gnagna\" or what classes actually uses those absolute reference paths. I've dropped this way of getting directories long ago since the day I moved to Win7 on another computer and all such applications I developped using that approach was doomed...

VB.NET Read Certain text in a text file

I want my program to read certain text in a text file. For example if I have a text file that contains the following info..
acc=blah
pass=hello
I want my vb.net application to get that the account variable is equal to blah, and the password variable is equal to hello.
Can anyone tell me how to do this?
Thanks
Here is a quick little bit of code that, after you click a button, will:
take an input file (in this case I created one called "test.ini")
read in the values as separate lines
do a search, using regular expressions, to see if it contains any "ACC=" or "PASS=" parameters
then write them to the console
here is the code:
Imports System.IO
Imports System.Text.RegularExpressions
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim strFile As String = "Test.INI"
Dim sr As New StreamReader(strFile)
Dim InputString As String
While sr.Peek <> -1
InputString = sr.ReadLine()
checkIfContains(InputString)
InputString = String.Empty
End While
sr.Close()
End Sub
Private Sub checkIfContains(ByVal inputString As String)
Dim outputFile As String = "testOutput.txt"
Dim m As Match
Dim m2 As Match
Dim itemPattern As String = "acc=(\S+)"
Dim itemPattern2 As String = "pass=(\S+)"
m = Regex.Match(inputString, itemPattern, _
RegexOptions.IgnoreCase Or RegexOptions.Compiled)
m2 = Regex.Match(inputString, itemPattern2, _
RegexOptions.IgnoreCase Or RegexOptions.Compiled)
Do While m.Success
Console.WriteLine("Found account {0}", _
m.Groups(1), m.Groups(1).Index)
m = m.NextMatch()
Loop
Do While m2.Success
Console.WriteLine("Found password {0}", _
m2.Groups(1), m2.Groups(1).Index)
m2 = m2.NextMatch()
Loop
End Sub
End Class
Have a look at this article
Reading and writing text files with VB.NET
Wile reading the file line by line, you can use String.Split Method with the splitter being "=", to split the string into param name, and param value.
Looks like you've got an INI file of some kind... The best way to read these is using the *PrivateProfile* functions of the windows API, which means you can actually have a proper full INI file quite easily for anything you need. There is a wrapper class here you may like to use.
Microsoft recommends that you use the registry to store this sort of information though, and discourages use of INI files.
If you wish to just use a file manually with the syntax you have, it is a simple case of splitting the string on '=' and put the results into a Dictionary. Remember to handle cases where the data was not found in the file and you need a default/error. In modern times though, XML is becoming a lot more popular for data text files, and there are lots of libraries to deal with loading from these.
My suggestion: you use XML. The .NET framework has a lot of good XML tools, if you're willing to make the transition to put all the text files into XML, it'll make life a lot easier.
Not what you're looking for, probably, but it's a cleaner solution than anything you could do with plain text (outside of developing your own parser or using a lower level API).
You can't really selectively read a certain bit of information in the file exclusively. You'll have to scan each line of the file and do a search for the string "pass=" at the beginning of the line. I don't know VB but look up these topics:
File readers (espically ones that can read one line at a time)
String tokenizers/splitting (as Astander mentioned)
File reading examples
Have you thought about getting the framework to handle it instead?
If you add an entry into the settings tab of the project properties with name acc, type string, scope user (or application, depending on requirements) and value pass, you can use the System.Configuration.ApplicationSettingsBase functionality to deal with it.
Private _settings As My.MySettings
Private _acc as String
Private _pass as String
Public ReadOnly Property Settings() As System.Configuration.ApplicationSettingsBase
Get
If _settings Is Nothing Then
_settings = New My.MySettings
End If
Return _settings
End Get
End Property
Private Sub SetSettings()
Settings.SettingsKey = Me.Name
Dim theSettings As My.MySettings
theSettings = DirectCast(Settings, My.MySettings)
theSettings.acc=_acc
theSettings.pass=_pass
Settings.Save()
End Sub
Private Sub GetSettings()
Settings.SettingsKey = Me.Name
Dim theSettings As My.MySettings
theSettings = DirectCast(Settings, My.MySettings)
_acc=theSettings.acc
_pass=theSettings.pass
End Sub
Call GetSettings in whatever load event you need, and SetSettings in closing events
This will create an entry in the application.exe.config file, either in your local settings \apps\2.0\etc etc directory, or your roaming one, or if it's a clickonce deployment, in the clickonce data directory. This will look like the following:-
<userSettings>
<MyTestApp.My.MySettings>
<setting name="acc" serializeAs="String">
<value>blah</value>
</setting>
<setting name="pass" serializeAs="String">
<value>hello</value>
</setting>
</MyTestApp.My.MySettings>
</userSettings>
Writing your own parser is not that hard. I managed to make one for a game (Using C#, but VB appears to have Regex class too. Using that, the acc variable in your file would be everything up to the = sign, and then blah would be everything past the = to the newline character (\n) Then go to the next line and repeat.
I have written this for you, check it and enjoy with the results, have a great day!
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim acc As New List(Of String)
Dim pass As New List(Of String)
Dim lines() As String = System.IO.File.ReadAllLines(".\credentials.txt")
For Each lineItem As String In lines
Dim vals() As String = lineItem.Split(Convert.ToChar("="))
If vals.Length > 0 Then
Dim lineId As String = vals(0)
If lineId = "acc" Then
acc.Add(vals(1))
ElseIf lineId = "pass" Then
pass.Add(vals(1))
End If
End If
Next
TextBox_acc.Text = String.Join(Environment.NewLine, acc)
TextBox_pass.Text = String.Join(Environment.NewLine, pass)
End Sub
End Class