Listview.ItemDrag files handling - vb.net

I am creating a repository for files I use daily at work, using a TabControl to hold multiple ListView holders. I have managed to create the Drag-Drop functionality from inside the ListView to outside my program using the following code inside the ListView.ItemDrag event handler:
Dim fileFullPath As String
Dim mySelectedItem As ListViewItem = lvTools.SelectedItems(0)
Dim dropList As New StringCollection
'DataObject requires a StringCollection
Dim DragPaths As New DataObject()
fileFullPath = Path.Combine(ToolsDirectory, mySelectedItem.Tag)
Try
'The fileInfo is created because it lets the DoDragDrop be faster
Dim fileToDrop As New FileInfo(fileFullPath)
dropList.Add(fileFullPath)
DragPaths.SetFileDropList(dropList)
DoDragDrop(DragPaths, DragDropEffects.All)
Catch FNTE As FileNotFoundException
MsgBox("File could not be found: " & fileFullPath)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
Do you have any idea how could I copy the contents of the .txt files instead of copying the files?

I figured this out, please refer to the following code for solution:
If File.Exists(fileFullPath) Then
Using tr As TextReader = New StreamReader(fileFullPath)
Dim fileToDrop As New FileInfo(fileFullPath)
dropList.Add(fileFullPath)
DragPaths.SetFileDropList(dropList)
Dim filecontent As String = tr.ReadToEnd()
DoDragDrop(filecontent, DragDropEffects.Copy)
End Using
End If

Related

Log Writer not creating new line for each entry

I get the feeling this is something really simple, but I've tried I don't know how many permutations of vbNewLine, Environment.NewLine, sMessage & vbNewLine (or Environment.Newline) I've tried, or how many pages on this site, or through Google I've looked at but nothing has worked.
I even tried getting help from a VB.Net discord channel I'm a part of and they suggested to do the same things that I've done and the procedure is still writing each new log entry at the end of the previous one in a continuous string. My writer is below. Am I missing something simple?
Edit: The code that worked is below in case anyone else comes along with the same issue. If you want to see the original code it's in the edit log.
Option Explicit On
Imports System.IO
Public Class WriteroLog
Public Shared Sub LogPrint(sMessage As String)
Dim AppPath As String = My.Application.Info.DirectoryPath
If File.Exists($"{AppPath}\Log.txt") = True Then
Try
Using objWriter As StreamWriter = File.AppendText($"{AppPath}\Log.Txt")
objWriter.WriteLine($"{Format(Now, "dd-MMM-yyyy HH:mm:ss")} – {sMessage}")
objWriter.Close()
End Using
Catch ex As Exception
MsgBox(ex)
Return
End Try
Else
Try
Using objWriter As StreamWriter = File.CreateText($"{AppPath}\Log.Txt")
objWriter.WriteLine($"{Format(Now, "dd-MMM-yyyy HH:mm:ss")} – {sMessage}")
objWriter.Close()
End Using
Catch ex As Exception
MsgBox(ex)
Return
End Try
End If
End Sub
End Class
The File.AppendText() method creates a new StreamWriter that is then used to append Text to the specified File.
Note, reading the Docs about this method, that you don't need to verify whether the File already exists: if it doesn't, the File is automatically created.
As a side note, when creating a Path, it's a good thing to use the Path.Combine method: it can prevent errors in the path definition and handles platform-specific formats.
Your code could be simplified as follows:
Public Shared Sub LogPrint(sMessage As String)
Dim filePath As String = Path.Combine(Application.StartupPath, "Log.Txt")
Try
Using writer As StreamWriter = File.AppendText(filePath)
writer.WriteLine($"{Date.Now.ToString("dd-MMM-yyyy HH:mm:ss")} – {sMessage}")
End Using
Catch ex As IOException
MsgBox(ex)
End Try
End Sub
The File.CreateText does not assign result to "objWrite", should be:
objWriter = File.CreateText($"{AppPath}\Log.Txt")
Not really sure if this is the root of your problem, but it is an issue.
In essences, your logic is re-opening or creating the stream "objWriter" for every call to this method. I would recommend you initialize "objWriter" to Nothing and only define if it is Nothing.
Set to Nothing as below.
Shared objWriter As IO.StreamWriter = Nothing
Then add check for Nothing in logic.

ByPassing System Files Permissions VB.Net

I started making a file manager which scans a folder selected by user through folderbrowserdialog in vb.net, extract the extentions of every file in every directory or subdirectory using GetFiles() Method with SearchOption.AllDirectories. I am having an UnauthorizedAccessException when it scans the Recycle Bin Folder in logical drive whereas i have seen Softwares like WinRar or R-Studio Scanning and showing every directory without having an exception or error. Following is my code:
Public Sub WriteExtentionsToFile()
Try
Title_Window.FolderBrowserDialog.ShowDialog()
Dim Path As String = Title_Window.FolderBrowserDialog.SelectedPath
'Getting FilesNames
Dim FileNames As String() = GetFilesNames(Path)
Title_Window.LblStatusBar.Text = "File Names Saved"
Dim MaximumForProgressBar As Integer = FileNames.Length
Title_Window.ToolStripProgressBar1.Maximum = MaximumForProgressBar
Title_Window.ToolStripProgressBar1.Step = 1
'Getting Extentions
Dim ExtentionsList As New List(Of String)
For Each item In FileNames
ExtentionsList.Add(System.IO.Path.GetExtension(item))
Next
'Deleting Duplicate Extentions
Dim NoDuplicateExtentionList As New List(Of String)
Dim ExtentionReadFromFile As String() = ReadExtentionsFromFile()'Another Function in my Class Which Reads previously saved extentions in a file and store them in an array
For Each item In ExtentionReadFromFile
NoDuplicateExtentionList.Add(item)
Next
For Each E In ExtentionsList
Dim LastIndexOfDot As Integer = E.LastIndexOf(".")
E = E.Remove(0, LastIndexOfDot + 1)
If NoDuplicateExtentionList.Contains(E.ToLower) = False Then
NoDuplicateExtentionList.Add(E.ToLower)
My.Computer.FileSystem.WriteAllText("d:\ExtentionsList.txt", E.ToString & vbCrLf, True)
End If
Next
MessageBox.Show("Writing Process Completed", "Success")
Catch ex As UnauthorizedAccessException
Dim Buttons As MessageBoxButtons = MessageBoxButtons.AbortRetryIgnore
Dim result As DialogResult
MessageBox.Show(ex.ToString, "Access Denied", Buttons)
If result = Windows.Forms.DialogResult.Ignore Then
'What should be Done Here????
ElseIf result = Windows.Forms.DialogResult.Abort Then
'What should be Done Here????
Else
'What should be Done Here????
End If
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub
What should be the solution for thsi problem? Is this about getting the permissions through FileIoPermission Class or something else??

Alternate ways of read, write data from multiple forms using vb.net

I am working on an application for work. We use an excel file to create reports. I am rusty in coding and really have not faced such a large request. The way the application works is there is standard toolbar. When the user opens the application he is presented with blank form. They must first create a new file and save it to their folder of choice. I have used the stream reader/writer but the problem is there is large amounts of data that user has to fillout so my code is horrible and monster like and I was wondering is there an easier way?
ToolStripLabel1.Text = FileDataStorage.OpenFileTextBox1.Text
If ToolStripLabel1.Text = ("C:\Temp\New QCA.qca") Then
MsgBox("Must Save New file first ACCESS DENIED!")
End If
Dim FILE_NAME As String = FileDataStorage.OpenFileTextBox1.Text
Try
If System.IO.File.Exists(FILE_NAME) = True Then
Dim objWriter As New System.IO.StreamWriter(FILE_NAME)
objWriter.WriteLine(General_Data.GTextBox1.Text)
objWriter.WriteLine(General_Data.GTextBox2.Text)
objWriter.WriteLine(General_Data.GTextBox3.Text)
objWriter.WriteLine(General_Data.GTextBox4.Text)
objWriter.WriteLine(General_Data.GTextBox5.Text)
objWriter.WriteLine(General_Data.GTextBox6.Text)
objWriter.WriteLine(General_Data.GTextBox7.Text)
objWriter.WriteLine(General_Data.GTextBox8.Text)
objWriter.WriteLine(General_Data.GTextBox9.Text)
objWriter.WriteLine(General_Data.GTextBox10.Text)
objWriter.WriteLine(General_Data.GTextBox11.Text)
objWriter.WriteLine(Inventory.ComboBox1.Text)
objWriter.WriteLine(Inventory.ComboBox2.Text)
objWriter.WriteLine(Inventory.ComboBox3.Text)
objWriter.WriteLine(Inventory.ComboBox4.Text)
objWriter.WriteLine(Inventory.ComboBox5.Text)
objWriter.WriteLine(Inventory.ComboBox6.Text)
objWriter.WriteLine(Inventory.ComboBox7.Text)
objWriter.WriteLine(Inventory.ComboBox8.Text)
objWriter.WriteLine(Inventory.ComboBox9.Text)
objWriter.WriteLine(Inventory.ComboBox10.Text)
objWriter.WriteLine(Inventory.ComboBox11.Text)
objWriter.WriteLine(Inventory.ComboBox12.Text)
objWriter.WriteLine(Inventory.ComboBox13.Text)
objWriter.WriteLine(Inventory.ComboBox14.Text)
objWriter.WriteLine(Inventory.ComboBox15.Text)
objWriter.WriteLine(Inventory.ComboBox16.Text)
objWriter.WriteLine(Inventory.ComboBox17.Text)
objWriter.WriteLine(Inventory.ComboBox18.Text)
objWriter.WriteLine(Inventory.ComboBox19.Text)
objWriter.WriteLine(Inventory.ComboBox20.Text)
objWriter.WriteLine(Inventory.TextBox1.Text)
objWriter.WriteLine(Inventory.TextBox2.Text)
objWriter.WriteLine(Inventory.TextBox3.Text)
objWriter.WriteLine(Inventory.TextBox4.Text)
objWriter.WriteLine(Inventory.TextBox5.Text)
objWriter.WriteLine(Inventory.TextBox6.Text)
objWriter.WriteLine(Inventory.TextBox7.Text)
objWriter.WriteLine(Inventory.TextBox8.Text)
objWriter.WriteLine(Inventory.TextBox9.Text)
objWriter.WriteLine(Inventory.TextBox10.Text)
objWriter.WriteLine(Inventory.TextBox11.Text)
objWriter.WriteLine(Inventory.TextBox12.Text)
objWriter.WriteLine(Inventory.TextBox13.Text)
objWriter.WriteLine(Inventory.TextBox14.Text)
objWriter.WriteLine(Inventory.TextBox15.Text)
objWriter.WriteLine(Inventory.TextBox16.Text)
objWriter.WriteLine(Inventory.TextBox17.Text)
objWriter.WriteLine(Inventory.TextBox18.Text)
objWriter.WriteLine(Inventory.TextBox19.Text)
objWriter.WriteLine(Inventory.TextBox20.Text)
objWriter.WriteLine(Inventory.TextBox21.Text)
objWriter.WriteLine(Inventory.TextBox22.Text)
objWriter.WriteLine(Inventory.TextBox23.Text)
objWriter.WriteLine(Inventory.TextBox24.Text)
objWriter.WriteLine(Inventory.TextBox25.Text)
objWriter.WriteLine(Inventory.TextBox26.Text)
objWriter.WriteLine(Inventory.TextBox27.Text)
objWriter.WriteLine(Inventory.TextBox28.Text)
objWriter.WriteLine(Inventory.TextBox29.Text)
objWriter.WriteLine(Inventory.TextBox30.Text)
objWriter.WriteLine(Inventory.TextBox31.Text)
objWriter.WriteLine(Inventory.TextBox32.Text)
objWriter.WriteLine(Inventory.TextBox33.Text)
objWriter.WriteLine(Inventory.TextBox34.Text)
objWriter.WriteLine(Inventory.TextBox35.Text)
objWriter.WriteLine(Inventory.TextBox36.Text)
objWriter.WriteLine(Inventory.TextBox37.Text)
objWriter.WriteLine(Inventory.TextBox38.Text)
objWriter.WriteLine(Inventory.TextBox39.Text)
objWriter.WriteLine(Inventory.TextBox40.Text)
objWriter.WriteLine(Inventory.TextBox41.Text)
objWriter.WriteLine(Inventory.TextBox42.Text)
objWriter.WriteLine(Inventory.TextBox43.Text)
objWriter.WriteLine(Inventory.TextBox44.Text)
objWriter.WriteLine(Inventory.TextBox45.Text)
objWriter.WriteLine(Inventory.TextBox46.Text)
objWriter.WriteLine(Inventory.TextBox47.Text)
objWriter.WriteLine(Inventory.TextBox48.Text)
objWriter.WriteLine(Inventory.TextBox49.Text)
objWriter.WriteLine(Inventory.TextBox50.Text)
objWriter.WriteLine(Inventory.TextBox51.Text)
objWriter.WriteLine(Inventory.TextBox52.Text)
objWriter.WriteLine(Inventory.TextBox53.Text)
objWriter.WriteLine(Inventory.TextBox54.Text)
objWriter.WriteLine(Inventory.TextBox55.Text)
objWriter.WriteLine(Inventory.TextBox56.Text)
objWriter.WriteLine(Inventory.TextBox57.Text)
objWriter.WriteLine(Inventory.TextBox58.Text)
objWriter.WriteLine(Inventory.TextBox59.Text)
objWriter.WriteLine(Inventory.TextBox60.Text)
objWriter.WriteLine(Inventory.TextBox61.Text)
objWriter.WriteLine(Inventory.TextBox62.Text)
objWriter.WriteLine(Inventory.TextBox63.Text)
objWriter.WriteLine(Inventory.TextBox64.Text)
objWriter.WriteLine(Inventory.TextBox65.Text)
objWriter.WriteLine(Inventory.TextBox66.Text)
objWriter.WriteLine(Inventory.TextBox67.Text)
objWriter.WriteLine(Inventory.TextBox68.Text)
objWriter.WriteLine(Inventory.TextBox69.Text)
objWriter.WriteLine(Inventory.TextBox70.Text)
objWriter.WriteLine(Inventory.TextBox71.Text)
objWriter.WriteLine(Inventory.TextBox72.Text)
objWriter.WriteLine(Inventory.TextBox73.Text)
objWriter.WriteLine(Inventory.TextBox74.Text)
objWriter.WriteLine(Inventory.TextBox75.Text)
objWriter.WriteLine(Inventory.TextBox76.Text)
objWriter.WriteLine(Inventory.TextBox77.Text)
objWriter.WriteLine(Inventory.TextBox78.Text)
objWriter.WriteLine(Inventory.TextBox79.Text)
objWriter.WriteLine(Inventory.TextBox80.Text)
objWriter.WriteLine(Water_QC.ComboBox1.Text)
objWriter.WriteLine(Water_QC.TextBox1.Text)
objWriter.WriteLine(Water_QC.TextBox2.Text)
objWriter.WriteLine(Water_QC.TextBox3.Text)
objWriter.WriteLine(Water_QC.TextBox4.Text)
objWriter.WriteLine(Water_QC.TextBox5.Text)
objWriter.WriteLine(Water_QC.ComboBox2.Text)
objWriter.WriteLine(Water_QC.TextBox6.Text)
objWriter.WriteLine(Water_QC.TextBox7.Text)
objWriter.WriteLine(Water_QC.TextBox8.Text)
objWriter.WriteLine(Water_QC.TextBox9.Text)
objWriter.WriteLine(Water_QC.TextBox10.Text)
objWriter.WriteLine(Water_QC.TextBox11.Text)
objWriter.WriteLine(Base_Gel_QC.TextBox1.Text)
objWriter.WriteLine(Base_Gel_QC.TextBox2.Text)
objWriter.WriteLine(Base_Gel_QC.TextBox3.Text)
objWriter.WriteLine(Base_Gel_QC.TextBox4.Text)
objWriter.WriteLine(Base_Gel_QC.TextBox5.Text)
objWriter.WriteLine(Base_Gel_QC.TextBox6.Text)
objWriter.WriteLine(Base_Gel_QC.TextBox7.Text)
objWriter.WriteLine(Base_Gel_QC.TextBox8.Text)
objWriter.WriteLine(Base_Gel_QC.TextBox9.Text)
objWriter.WriteLine(Base_Gel_QC.TextBox10.Text)
objWriter.WriteLine(Base_Gel_QC.TextBox11.Text)
objWriter.WriteLine(Base_Gel_QC.TextBox12.Text)
objWriter.WriteLine(Base_Gel_QC.TextBox13.Text)
objWriter.WriteLine(Base_Gel_QC.TextBox14.Text)
objWriter.WriteLine(Base_Gel_QC.TextBox15.Text)
objWriter.WriteLine(Base_Gel_QC.TextBox16.Text)
objWriter.WriteLine(Base_Gel_QC.TextBox17.Text)
objWriter.WriteLine(Base_Gel_QC.TextBox18.Text)
objWriter.WriteLine(Base_Gel_QC.TextBox19.Text)
objWriter.WriteLine(Base_Gel_QC.TextBox20.Text)
objWriter.WriteLine(Base_Gel_QC.TextBox21.Text)
objWriter.WriteLine(Base_Gel_QC.TextBox22.Text)
objWriter.WriteLine(Base_Gel_QC.TextBox23.Text)
objWriter.WriteLine(Base_Gel_QC.TextBox24.Text)
objWriter.WriteLine(Base_Gel_QC.TextBox25.Text)
objWriter.WriteLine(Base_Gel_QC.TextBox26.Text)
objWriter.WriteLine(Base_Gel_QC.TextBox27.Text)
objWriter.WriteLine(Base_Gel_QC.TextBox28.Text)
objWriter.WriteLine(Base_Gel_QC.TextBox29.Text)
objWriter.WriteLine(Base_Gel_QC.TextBox30.Text)
objWriter.WriteLine(Base_Gel_QC.TextBox31.Text)
objWriter.WriteLine(Base_Gel_QC.TextBox32.Text)
objWriter.WriteLine(Base_Gel_QC.TextBox33.Text)
objWriter.WriteLine(Base_Gel_QC.TextBox34.Text)
objWriter.WriteLine(Base_Gel_QC.TextBox35.Text)
objWriter.WriteLine(Acid_QC.TextBox1.Text)
objWriter.WriteLine(Acid_QC.TextBox2.Text)
objWriter.WriteLine(Acid_QC.TextBox3.Text)
objWriter.WriteLine(Acid_QC.TextBox4.Text)
objWriter.WriteLine(Acid_QC.TextBox5.Text)
objWriter.WriteLine(Acid_QC.TextBox6.Text)
objWriter.WriteLine(Acid_QC.TextBox7.Text)
objWriter.WriteLine(Acid_QC.TextBox8.Text)
objWriter.WriteLine(Acid_QC.TextBox9.Text)
objWriter.WriteLine(Acid_QC.TextBox10.Text)
objWriter.WriteLine(Acid_QC.TextBox11.Text)
objWriter.WriteLine(Acid_QC.TextBox12.Text)
objWriter.WriteLine(Acid_QC.TextBox13.Text)
objWriter.WriteLine(Acid_QC.TextBox14.Text)
objWriter.WriteLine(Acid_QC.TextBox15.Text)
objWriter.WriteLine(Acid_QC.TextBox16.Text)
objWriter.WriteLine(Acid_QC.TextBox17.Text)
objWriter.WriteLine(Acid_QC.TextBox18.Text)
objWriter.WriteLine(Acid_QC.TextBox19.Text)
objWriter.WriteLine(Acid_QC.TextBox20.Text)
objWriter.WriteLine(Acid_QC.TextBox21.Text)
objWriter.WriteLine(Acid_QC.TextBox22.Text)
objWriter.WriteLine(Acid_QC.TextBox23.Text)
objWriter.WriteLine(Acid_QC.TextBox24.Text)
objWriter.WriteLine(Acid_QC.TextBox25.Text)
objWriter.WriteLine(Acid_QC.TextBox26.Text)
objWriter.WriteLine(Acid_QC.TextBox27.Text)
objWriter.WriteLine(Acid_QC.TextBox28.Text)
objWriter.WriteLine(Acid_QC.TextBox29.Text)
objWriter.WriteLine(Acid_QC.TextBox30.Text)
objWriter.WriteLine(Acid_QC.TextBox31.Text)
objWriter.WriteLine(Acid_QC.TextBox32.Text)
objWriter.WriteLine(Acid_QC.TextBox33.Text)
objWriter.WriteLine(Acid_QC.TextBox34.Text)
objWriter.WriteLine(Acid_QC.TextBox35.Text)
objWriter.WriteLine(Acid_QC.TextBox36.Text)
objWriter.WriteLine(Acid_QC.TextBox37.Text)
objWriter.WriteLine(Acid_QC.TextBox38.Text)
objWriter.WriteLine(Acid_QC.TextBox39.Text)
objWriter.WriteLine(Acid_QC.TextBox40.Text)
objWriter.WriteLine(Acid_QC.TextBox41.Text)
objWriter.WriteLine(Acid_QC.TextBox42.Text)
objWriter.WriteLine(Acid_QC.TextBox43.Text)
objWriter.WriteLine(Acid_QC.TextBox44.Text)
objWriter.WriteLine(Acid_QC.TextBox45.Text)
objWriter.WriteLine(Acid_QC.TextBox46.Text)
objWriter.WriteLine(Acid_QC.TextBox47.Text)
objWriter.WriteLine(Acid_QC.TextBox48.Text)
objWriter.WriteLine(Acid_QC.TextBox49.Text)
objWriter.WriteLine(Acid_QC.TextBox50.Text)
objWriter.WriteLine(Acid_QC.TextBox51.Text)
objWriter.WriteLine(Acid_QC.TextBox52.Text)
objWriter.WriteLine(Acid_QC.TextBox53.Text)
objWriter.WriteLine(Acid_QC.TextBox54.Text)
objWriter.WriteLine(Acid_QC.TextBox55.Text)
objWriter.WriteLine(Acid_QC.TextBox56.Text)
objWriter.WriteLine(Acid_QC.TextBox57.Text)
objWriter.WriteLine(Acid_QC.TextBox58.Text)
objWriter.WriteLine(Acid_QC.TextBox59.Text)
objWriter.WriteLine(Acid_QC.TextBox60.Text)
objWriter.WriteLine(Acid_QC.TextBox61.Text)
objWriter.WriteLine(Acid_QC.TextBox62.Text)
objWriter.WriteLine(Acid_QC.TextBox63.Text)
objWriter.WriteLine(Acid_QC.TextBox64.Text)
objWriter.WriteLine(Acid_QC.TextBox65.Text)
objWriter.Close()
MsgBox("Data written to file")
Else
'Do Nothing
End If
Catch ex As Exception
End Try
End Sub
If you mean making the code more elegant? You could do something like the following.
ToolStripLabel1.Text = FileDataStorage.OpenFileTextBox1.Text
If ToolStripLabel1.Text = ("C:\Temp\New QCA.qca") Then
MsgBox("Must Save New file first ACCESS DENIED!")
End If
Dim FILE_NAME As String = FileDataStorage.OpenFileTextBox1.Text
Try
If System.IO.File.Exists(FILE_NAME) = True Then
Dim objWriter As New System.IO.StreamWriter(FILE_NAME)
For Each c As Control In Form.Controls
If TypeOf c Is TextBox Or TypeOf c Is ComboBox Then
objWriter.WriteLine(c.Text)
End If
Next
objWriter.Close()
MsgBox("Data written to file")
Else
'Do Nothing
End If
Catch ex As Exception
Throw
End Try
In keeping with what you have already, an external function similar to this may work for you.
It goes over the form passed to it, and then finds the objects of a certain type, and then prints them out to the writer that you've created there. You'll have to make it work with what you have already, but something like this would save so many lines.
What it does is gets the number of controls on the form, and then goes across each one to get its type. If the type is correct, then it just writes its contents to the file.
Private sub writeToFile(formFrom as Form)
Dim controls As Control
For Each controls In formFrom.Controls
If TypeOf (controls) Is TextBox Then
objWriter.WriteLine(formFrom.controls.text)
elseif TypeOf (controls) is ComboBox then
objWriter.WriteLine(formFrom.controls.text)
End If
Next
end sub

Cannot write to a file after reading it with IO.StreamReader

I am making an encryption program that also functions as a notepad (CryptoMatic). There is a Open function with the following code:
Dim r As New StreamReader(filename)
Dim text As String = r.ReadToEnd
txtFileContents.text = text
r.Dispose()
r.Close()
Now there is the save and save as button
the save button checks if the file had been saved previously or not. If yes then I am trying to write to that file with the following code:
Dim myFileStream As New System.IO.FileStream(filename, FileMode.Append, FileAccess.Write, FileShare.None)
myFileStream.Lock(0, 100)
Dim myWriter As New System.IO.StreamWriter(myFileStream)
myWriter.WriteLine(text)
myWriter.Flush()
myWriter.Close()
myFileStream.Close()
myFileStream.Unlock(0, 100)
Now when i press the save button it gives the following error:
The process cannot access the file 'C:\Users\VisualTech\Documents\Visual Studio 2010\Projects\CryptoMatic Update\CryptoMatic Update\bin\Debug\text.txt' because it is being used by another process.
and the application stops, can anyone please help me?
I have tried your code above and have noticed two things:
You don't need a myFileStream.Lock call because you already open it
with FileShare.None
You can't call myFileStream.Unlock(0, 100) after the call to
myFileStream.Close()
So I have rewritten your code in this way.
Sub Main
Dim text = "This is a text to write in the stream"
Using myFileStream As New System.IO.FileStream("D:\temp\testlock.txt", FileMode.Append, FileAccess.Write, FileShare.None)
Using myWriter As New System.IO.StreamWriter(myFileStream)
myWriter.WriteLine(text)
End Using
End Using
End Sub

Why am I getting object reference not set error on script task connector?

I have an SSIS package (SQL Server 2005) that loops through a bunch of flat files in a folder. I need to wait until the source application has finished writing the file before I can open it in my flat file import task.
I have a For Each loop container and within it a script task to execute before the Data Flow Task.
When I try to create the success connector between the Script Task and the Data Flow Task I get this error:
Could not create connector. Object reference not set to an instance of
an object.
I get that something is being set to nothing, but I can't see it. I have DelayValidation set to true on both the Script Task and the Data Flow Task. What else am I missing?
I'm a C# guy so maybe I'm missing something obvious in the VB. Here's the script I poached from the interwebs:
Public Sub Main()
Dim strFileName As String = CType(Dts.Variables("FileName").Value, String)
Dim objFS As System.IO.FileStream
Dim bolFinished As Boolean = False
Do
Try
objFS = System.IO.File.Open(strFileName, FileMode.Open, FileAccess.ReadWrite, FileShare.None)
bolFinished = True
objFS.Close()
Catch ex As Exception
System.Threading.Thread.Sleep(1000)
End Try
Loop
If bolFinished Then
Dts.TaskResult = Dts.Results.Success
Else
Dts.TaskResult = Dts.Results.Failure
End If
End Sub
Milen k is more than right. It looks like you have an infinite loop which is opening a file several times until it breaks down.
You could change your code with the below suggested code. This will help you to get out of the infinite loop.
Your current code:
Do
Try
objFS = System.IO.File.Open(strFileName, FileMode.Open, FileAccess.ReadWrite, FileShare.None)
bolFinished = True
objFS.Close()
Catch ex As Exception
System.Threading.Thread.Sleep(1000)
End Try
Loop
Suggested code:
Do While(true)
Try
objFS = System.IO.File.Open(strFileName, FileMode.Open, FileAccess.ReadWrite, FileShare.None)
bolFinished = True
objFS.Close()
Exit Do
Catch ex As Exception
System.Threading.Thread.Sleep(1000)
End Try
Loop
Make sure that you have created a Flat File Source for your Data Flow task. If you do not have an existing one, create a temporary one which act as a place-holder for the file paths you feed it through the For Each loop.
From what I understand, you should be passing the path to each file that you will be importing to your Flat File Connection. This can easily be done by adding the variable generated in your For Each loop as an expression in the Expression property of your Flat File Connection.
UPDATE:
You need to set a condition in your Do ... Loop. For example: Loop While Not bolFinished. Look at this document for more information.