Not saving .txt file in vb.net - vb.net

Cutting to the chase:
Function Create(ByVal network, ByVal location, ByVal type, ByVal requirement1, ByVal requirement2, ByVal requirement3, ByVal name)
Dim net As String = network
Dim loc As String = location
Dim typ As String = type
Dim nam As String = name
Dim req1 As String = requirement1
Dim req2 As String = requirement2
Dim req3 As String = requirement3
Dim Mission As New System.IO.StreamWriter("C:\" & nam & ".txt")
Mission.WriteLine("Name: " & net)
Mission.WriteLine("Network: " & net)
Mission.WriteLine("Location: " & loc)
Mission.WriteLine("Type: " & typ)
Mission.WriteLine("Requirement: " & req1)
Mission.WriteLine("Requirement: " & req2)
Mission.WriteLine("Requirement: " & req3)
Mission.Close()
Console.WriteLine("Written")
System.Threading.Thread.Sleep(3000)
End Function
No errors appear, but neither does the file in the filepath. I have it all declared when I call the function, so I know that its not the problem.
Help? :)

Sub Create(ByVal network As String, ByVal location As String,
ByVal type As String, ByVal requirement1 As String,
ByVal requirement2 As String, ByVal requirement3 As String,
ByVal name As String)
Dim net As String = network
Dim loc As String = location
Dim typ As String = type
Dim nam As String = name
Dim req1 As String = requirement1
Dim req2 As String = requirement2
Dim req3 As String = requirement3
If Not nam.EndsWith(".txt") Then
nam &= ".txt"
End If
Dim Mission As New System.IO.StreamWriter(Path.Combine(
System.Environment.GetFolderPath(
System.Environment.SpecialFolder.Desktop), nam))
Mission.WriteLine("Name: " & net)
Mission.WriteLine("Network: " & net)
Mission.WriteLine("Location: " & loc)
Mission.WriteLine("Type: " & typ)
Mission.WriteLine("Requirement: " & req1)
Mission.WriteLine("Requirement: " & req2)
Mission.WriteLine("Requirement: " & req3)
Mission.Close()
Console.WriteLine("Written")
'System.Threading.Thread.Sleep(3000)
End Sub
first of all pal, you were passing the parameters as objects!!, second you dont have permission to save to c root directory unless you have administrator privilege, third the threading part at the end is completely useless, its blocking the UI and don't add up to any benefit.
at last but not least.
good luck

Related

Converting ImageCombo from VB6 to Vb.Net shows simple non Editable Textbox

I have the following code written in Vb6 for ImageCombo control.
Begin MSComctlLib.ImageCombo dImageCombo
Height = 330
Index = 0
Left = 120
TabIndex = 10
Top = 3480
Visible = 0 'False
Width = 1815
_ExtentX = 3201
_ExtentY = 582
_Version = 393216
ForeColor = -2147483640
BackColor = -2147483643
Text = "ImageCombo1"
End
Then I have a RenderCombo function as follows. It goes through each and every line of function and get a proper value as well for ItemList in ImageCombo but doesn't display anything. It simply appear as a Non Editable Textview.
Private Sub RenderCombo(a_dbmgr As ObjectDBManager, _
ByRef a_X As Integer, ByRef a_Y As Integer, _
bnode As DataSetNode, fldname As String, _
MaxPixHeight As Integer, ColMaxWidth As Integer, _
hLevels As Integer, YStart As Integer, gHeight As Integer, _
padding As Integer, gInframe As Boolean, a_tooltip As String)
Dim tstr As String
Dim ltmp As ComboItem
Dim lseldata As String
Dim loldcolwidth As Integer
Load dImageCombo(nCombos + 1)
nCombos = nCombos + 1
tstr = bnode.GetLeafString("WIDTH")
If tstr <> "" Then
dImageCombo(nCombos).Width = SafeCLng(tstr)
End If
dImageCombo(nCombos).Visible = True
dImageCombo(nCombos).Left = a_X
dImageCombo(nCombos).Top = a_Y
lseldata = bnode.GetLeafString("SELTABLENAV") & "+" & _
bnode.GetLeafString("SELMATCHFIELD") & "+" & _
bnode.GetLeafString("SELMATCHNAV") & "+" & _
bnode.GetLeafString("SELLABELNAME") & "+" & _
bnode.GetLeafString("SELNOBLANK") & "+" & _
bnode.GetLeafString("SELVALCAPS") & "+"
dImageCombo(nCombos).Tag = fldname & "+" & lseldata
mMakeComboList a_dbmgr, bnode, nCombos, lseldata
SetNextXY a_X, a_Y, dImageCombo(nCombos).Width, dImageCombo(nCombos).Height, _
MaxPixHeight, gHeight, hLevels, YStart, ColMaxWidth, loldcolwidth, "COMBO", gInframe
If gInframe Then
dFrame(nFrames).Width = mBumpWidth(dFrame(nFrames).Width, dImageCombo(nCombos).Left + loldcolwidth + padding, "COMBO")
Set dImageCombo(nCombos).Container = dFrame(nFrames)
End If
dImageCombo(nCombos).ToolTipText = a_tooltip
End Sub
Code that I've got in VB.Net after migration is as follows:
Private Sub RenderCombo(ByRef a_dbmgr As ObjectDBManager, ByRef a_X As Short, ByRef a_Y As Short, ByRef bnode As DataSetNode, ByRef fldname As String, ByRef MaxPixHeight As Short, ByRef ColMaxWidth As Short, ByRef hLevels As Short, ByRef YStart As Short, ByRef gHeight As Short, ByRef padding As Short, ByRef gInframe As Boolean, ByRef a_tooltip As String)
Dim tstr As String = ""
Dim ltmp As MSComctlLib.ComboItem
Dim lseldata As String = ""
Dim loldcolwidth As Short
dImageCombo.Load(nCombos + 1)
nCombos = nCombos + 1
tstr = bnode.GetLeafString("WIDTH")
If tstr <> "" Then
dImageCombo(nCombos).Width = SafeCLng(tstr)
End If
dImageCombo(nCombos).Visible = True
dImageCombo(nCombos).Left = a_X
dImageCombo(nCombos).Top = a_Y
lseldata = bnode.GetLeafString("SELTABLENAV") & "+" & bnode.GetLeafString("SELMATCHFIELD") & "+" & bnode.GetLeafString("SELMATCHNAV") & "+" & bnode.GetLeafString("SELLABELNAME") & "+" & bnode.GetLeafString("SELNOBLANK") & "+" & bnode.GetLeafString("SELVALCAPS") & "+"
dImageCombo(nCombos).Tag = fldname & "+" & lseldata
mMakeComboList(a_dbmgr, bnode, nCombos, lseldata)
SetNextXY(a_X, a_Y, dImageCombo(nCombos).Width, dImageCombo(nCombos).Height, MaxPixHeight, gHeight, hLevels, YStart, ColMaxWidth, loldcolwidth, "COMBO", gInframe)
If gInframe Then
dFrame(nFrames).Width = mBumpWidth(dFrame(nFrames).Width, dImageCombo(nCombos).Left + loldcolwidth + padding, "COMBO")
dImageCombo(nCombos).Parent = dFrame(nFrames)
End If
ToolTip1.SetToolTip(dImageCombo(nCombos), a_tooltip)
End Sub
Designer code after migration:
Friend WithEvents dImageCombo As AxImageComboArray
Friend WithEvents _dImageCombo_0 As AxMSComctlLib.AxImageCombo
Me._dImageCombo_0 = New AxMSComctlLib.AxImageCombo
Me.dImageCombo = New AxImageComboArray(Me.components)
CType(Me._dImageCombo_0, System.ComponentModel.ISupportInitialize).BeginInit()
CType(Me.dImageCombo, System.ComponentModel.ISupportInitialize).BeginInit()
Me.dImageCombo.SetIndex(Me._dImageCombo_0, CType(0, Short))
Me._dImageCombo_0.Location = New System.Drawing.Point(8, 232)
Me._dImageCombo_0.Name = "_dImageCombo_0"
Me._dImageCombo_0.OcxState = CType(resources.GetObject("_dImageCombo_0.OcxState"), System.Windows.Forms.AxHost.State)
Me._dImageCombo_0.Size = New System.Drawing.Size(121, 22)
Me._dImageCombo_0.TabIndex = 10
Me._dImageCombo_0.Visible = False
Me.Controls.Add(Me._dImageCombo_0)
CType(Me._dImageCombo_0, System.ComponentModel.ISupportInitialize).EndInit()
CType(Me.dImageCombo, System.ComponentModel.ISupportInitialize).EndInit()
Here is the screenshot of ImageCombo that display as a simple TextBox in Vb.Net.
Any help would be appreciated.

Find a line by searching for a string, then edit a line which is 2 lines below

My first question here, I'll try to be as clear as possible.
I have a text file that looks like this :
[...]
"tickets"
{
"436" "320000000400000083421a060100100104"
"674" "320000000400000083421a06010010010a"
"292" "320000000400000083421a0601001001f0"
"551" "320000000400000083421a0601001001da"
}
"99550"
{
"informations" "254"
"parameters" "-banana -lemon"
}
"99551"
{
"informations" "641"
"parameters" "-banana -lemon"
}
"550"
{
"informations" "551"
"parameters" "-banana -lemon"
}
"551"
{
"informations" "123"
"parameters" "-banana -lemon"
}
"552"
{
"informations" "987"
"parameters" "-banana -lemon"
}
[...]
What I want to do is:
search for this string in the text file :
"551"
{
add -apple to the parameters line which is 2 lines below, should look like this :
"parameters" "-banana -lemon -apple"
I think it's the only way to find this line, but I don't get the coding skills to get it done.
You can try something like this:
Function ReplaceSpecial(ByVal text As String, ByVal find As String, ByVal insert As String) As String
Dim allLines() As String = Split(text, vbCrLf)
Dim lineNumber = Array.IndexOf(allLines, find)
Dim lineToUpdate = Array.FindIndex(Of String)(allLines, lineNumber, Function(f) f.Trim.StartsWith("""parameters"""))
allLines(lineToUpdate) = allLines(lineToUpdate).Trim.Substring(0, allLines(lineToUpdate).Length - 1) & " " & insert & """"
Return Join(allLines, vbCrLf)
End Function
Sample Usage:
Assuming that your input text is in a textbox named InputTextBox the following code will show you updated text in OutputTextBox.
OutputTextBox.Text = ReplaceSpecial(InputTextBox.text, """550""", "-apple")
Here's another one, not much different than Pradeep Kumar's submission:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim FileName As String = "C:\Users\Mike\Documents\SomeFile.txt"
Dim lines As New List(Of String)(File.ReadAllLines(FileName))
AddParameterToSection(lines, "551", "-apple")
AddParameterToSection(lines, "552", "-orange")
File.WriteAllLines(FileName, lines)
End Sub
Private Sub AddParameterToSection(ByVal lines As List(Of String), ByVal sectionID As String, ByVal parameter As String)
Static DoubleQuote As String = Chr(34)
Static Parameters As String = DoubleQuote & "parameters" & DoubleQuote
Static PairOfDoubleQoutes As String = DoubleQuote & DoubleQuote
Dim header As String = DoubleQuote & sectionID & DoubleQuote
Dim index As Integer = lines.FindIndex(Function(x) x.Trim() = header)
If index <> -1 Then
Dim endBracket As Integer = lines.FindIndex(index, Function(x) x.Trim() = "}")
Dim paramIndex As Integer = lines.FindIndex(index, Function(x) x.Trim().StartsWith(Parameters))
If paramIndex <> -1 AndAlso paramIndex < endBracket Then
If lines(paramIndex).EndsWith(PairOfDoubleQoutes) Then
lines(paramIndex) = lines(paramIndex).Replace(PairOfDoubleQoutes, DoubleQuote & parameter & DoubleQuote)
Else
lines(paramIndex) = lines(paramIndex).TrimEnd(DoubleQuote) & " " & parameter & DoubleQuote
End If
End If
End If
End Sub

Array affected after sending it to a function

I send an arrey with the length of 26 twice to a function like this:
....
SaveXML strXmlItem, DFCCM, SCTM, Zieltabelle
SaveXML strXmlItem, DFCCS, SCTS, Zieltabelle
....
After the first invokation:
SaveXML strXmlItem, DFCCM, SCTM, Zieltabelle
Content of strXmlItem are changed. This array with other variablesis definedlike this:
Dim strSql, strXmlItem(), strA2l, strHex As String
and the function is:
Private Function SaveXML(strarr(), DFCC As String, SCT As String, ByVal Zieltabelle As String)
strarr(4) = DFCC
strarr(6) = SCT
For K = 0 To UBound(strarr)
MsgBox strarr(K)
Next K
'XML-Syntax anpassen
For J = 1 To conAnzahlFelder - 1
strarr(J) = MakeQuotes(strarr(J)) & ", "
Next J
strarr(conAnzahlFelder) = MakeQuotes(strarr(conAnzahlFelder)) & ")"
' Anfügeabfrage zusammenbasteln
strSql = "Insert Into " & Zieltabelle & " ("
For J = 1 To conAnzahlFelder
strSql = strSql & "f" & Trim(CStr(J - 1)) & ", "
Next J
strSql = strSql & "f" & Trim(CStr(conAnzahlFelder)) & ") "
strSql = strSql & "Values ("
For J = 1 To conAnzahlFelder + 1
strSql = strSql & strarr(J - 1)
Next J
MsgBox strSql
DoCmd.RunSQL strSql
End Function
How can I solve the problem?
there are a lot of issues in your code. But to answer your question
first:
try:
Private Function SaveXML(ByVal strarr(), DFCC As String, SCT As String, ByVal Zieltabelle As String)
instead of
Private Function SaveXML(strarr(), DFCC As String, SCT As String, ByVal Zieltabelle As String)
Google for the difference between ByVal and ByRef.
Second:
you really should use Option Explicit on the beginning of each module. You use a lot of undeclared variables. This can cause additional issues.
Third:
Dim strSql, strXmlItem(), strA2l, strHex As String
generates only ONE string variable (strHex). The others are declared as Variant if you use that Syntax. This works in some other languages but not in VBA!
and last but not least:
you declare a Function but you do not return any value for it. Either you do not want to return something (then you shouldn't declare it as Function) or you should provide a return value.

VB.net lag across different machines

I wrote a small little key inventory program. Basically it uses an INI file to know what keys we have and which ones are signed out, to whom, by whom, etc. It is run on up to 4 computers all in the same room and accesses the ini file on our local server.
When I sign a key out or in, it shows the change instantly. If I run two instances of the same program on the same machine, again, it's instant.
When I sign out a key on machine A and a co-worker is running the same program on machine B, C or D, the change in what is signed out doesn't show up for 4-10 seconds.
The way the program checks for updates is by using IO.File.Getlastwritetime on loading (saving it to a label with visible = false) and then every time the timer goes (every second) it compares the files "getlastwritetime" to the label. If they are different, then it updates, if they are the same, it does nothing.
Any idea where I should start looking for this lag? Could it be a server problem?
Here is the code for Timer1.tick, I am using the read/write ini codes from http://deepvbnet.blogspot.in/2008/07/how-to-read-from-ini-file.html
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim TimeCheck As String = IO.File.GetLastWriteTime("G:/BFAS/DPS/Comm Center/TEST/keys.ini")
If TimeCheck <> Label5.Text Then
ListBox1.Items.Clear()
ComboBox1.Items.Clear()
str1 = "NumOfKeys"
Call readIniFile()
Dim OneTime As String = strdata.ToString
Dim numberofkeys As Integer = Convert.ToInt32(OneTime)
For i = 1 To numberofkeys
str1 = "Key" & i & "Out"
Call readIniFile()
Dim isKeyOut As String = strdata.ToString
If isKeyOut = "True" Then
str1 = "Key" & i & "Name"
Call readIniFile()
Dim KeyName As String = strdata.ToString
str1 = "Key" & i & "Unit"
Call readIniFile()
Dim string1 As String = strdata.ToString
str1 = "Key" & i & "Rent"
Call readIniFile()
Dim string2 As String = strdata.ToString
str1 = "Key" & i & "User"
Call readIniFile()
Dim string3 As String = strdata.ToString
str1 = "Key" & i & "Date"
Call readIniFile()
Dim string4 As String = strdata.ToString
str1 = "Key" & i & "Time"
Call readIniFile()
Dim string5 As String = strdata.ToString
ListBox1.Items.Add(LSet(KeyName, 20) + " - " + string1 + " - " + string2 + " - " + string3 + " - " + string4 + " - " + string5)
ElseIf isKeyOut = "False" Then
str1 = "Key" & i & "Name"
Call readIniFile()
Dim thisKeysName As String = strdata.ToString
ComboBox1.Items.Add(thisKeysName)
End If
Next
Dim FileTime As String = IO.File.GetLastWriteTime("G:/BFAS/DPS/Comm Center/TEST/keys.ini")
Label5.Text = FileTime
End If
Dim escortTime As String = IO.Directory.GetLastWriteTime("G:/BFAS/DPS/Comm Center/TEST/Escort")
If escortTime <> Label7.Text Then
ListBox2.Items.Clear()
For Each foundfile In My.Computer.FileSystem.GetFiles("G:/BFAS/DPS/Comm Center/TEST/Escort/")
If foundfile = "G:/BFAS/DPS/Comm Center/TEST/Escorthistory.txt" Then
'do nothing
Else
Dim Infomation As String = My.Computer.FileSystem.ReadAllText(foundfile)
ListBox2.Items.Add(Infomation)
End If
Next
Label7.Text = IO.Directory.GetLastWriteTime("G:/BFAS/DPS/Comm Center/TEST/Escort")
End If
Dim alarmtime As String = IO.Directory.GetLastWriteTime("G:/BFAS/DPS/Comm Center/TEST/Alarms")
If alarmtime <> Label8.Text Then
ListBox3.Items.Clear()
For Each foundfile In My.Computer.FileSystem.GetFiles("G:/BFAS/DPS/Comm Center/TEST/Alarms/")
Dim Infomation As String = My.Computer.FileSystem.ReadAllText(foundfile)
ListBox3.Items.Add(Infomation)
Next
Label8.Text = IO.Directory.GetLastWriteTime("G:/BFAS/DPS/Comm Center/TEST/Alarms")
End If
Dim turnovertime As String = IO.File.GetLastWriteTime("G:/BFAS/DPS/Comm Center/TEST/turnover.txt")
If Label9.Text <> turnovertime Then
Dim newTO As String = My.Computer.FileSystem.ReadAllText("G:/BFAS/DPS/Comm Center/TEST/turnover.txt")
TextBox3.Text = newTO
Label9.Text = IO.File.GetLastWriteTime("G:/BFAS/DPS/Comm Center/TEST/turnover.txt")
End If
End Sub

Converting Image VB.net

I have the following code
Public Sub ConvertImage(ByVal Filename As String, _
ByVal DesiredFormat As System.Drawing.Imaging.ImageFormat, _
ByVal NewFilename As String)
' Takes a filename and saves the file in a new format
Try
Dim imgFile As System.Drawing.Image = _
System.Drawing.Image.FromFile(Filename)
imgFile.Save(NewFilename, DesiredFormat)
Catch ex As Exception
Throw ex
End Try
End Sub
Private Sub btnPrintVC40_Click(sender As System.Object, e As System.EventArgs) Handles btnPrintVC40.Click
Dim SigName As String
Dim SigNameNew As String
SigName = "SELECT Signature FROM vw_Report_VehicleCheckFTA WHERE VCID = " & CStr(lRiskAssessID)
SigNameNew = SigName.PadLeft(SigName.Length - 4)
ConvertImage(SigName, _
System.Drawing.Imaging.ImageFormat.Jpeg, _
SigNameNew & ".jpeg")
runReport("SELECT * FROM vw_Report_VehicleCheckFTA WHERE VCID = " & CStr(lRiskAssessID), "FTAVehicleCheck2.rpt")
End Sub
Basically what this should do is convert a png image to jpeg, then launch a crystal report. Instead I get the following exception:
I just can't for the life of me figure out why.
SigName = "SELECT Signature FROM vw_Report_VehicleCheckFTA WHERE VCID = " & CStr(lRiskAssessID) ' lRiskAssessID is 3772 I guess
SigNameNew = SigName.PadLeft(SigName.Length - 4) 'SignameNew will become "SELECT Signature FROM vw_Report_VehicleCheckFTA WHERE VCID = 3772", PadLeft will actually do nothing at all
ConvertImage(SigName, _
System.Drawing.Imaging.ImageFormat.Jpeg, _
SigNameNew & ".jpeg")
Convert Image is called with Filename "SELECT Signature FROM vw_Report_VehicleCheckFTA WHERE VCID = 3772" and I guess that's not the real filename.
You should take a look on how to get data from your database, you can't just throw some string in the air to get it ;)