Start debugging from add-in - visual-studio-debugging

I'm making VS add-in which changes debugging commandline.
This part works, but i need to start debugging after it's changed.
How do I start debugging from add-in?
mybe something from this function might help
Function GetCommandArgsProperty() As EnvDTE.Property
Dim solution As Solution
Dim project As Project
Dim sb As SolutionBuild
Dim cm As ConfigurationManager
Dim config As Configuration
Dim properties As Properties
Dim prop As EnvDTE.Property
solution = _applicationObject.Solution
sb = solution.SolutionBuild
For Each str As String In sb.StartupProjects
project = solution.Item(str)
cm = project.ConfigurationManager
config = cm.ActiveConfiguration
properties = config.Properties
For Each prop In properties
If prop.Name = "CommandArguments" Then
Return prop
End If
Next
Next
End Function

got it working
Dim dte As EnvDTE.DTE = GetService(GetType(EnvDTE.DTE))
dte.Solution.SolutionBuild.Run()

Related

Putting column names from database table into a list

I am trying to put columns names from a table inside a Microsoft Access database inside a list variable. I have done this so far but the line where I am trying to add it to the topic variable does not work and is coming up with the error
predefined type ‘valuetuple(of,,,)’ is not defined or imported.
The code is:
Dim topic = topic()
Dim filtervalues = {Nothing, Nothing, "Results", Nothing}
Dim counter As Integer = 0
Using con = _
New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Database.mdb")
Dim columns = con.GetSchema("columns", filtervalues)
For Each row As DataRow In columns.Rows
topic(counter) = ("{0,-20}{1}", row("column_name"), row("data_type"))
counter = +1
Next
End Using
According to the documentation on Value Tupels you must get the NuGet package System.ValueTuple, if you are working with Framework version prior to 4.7:
Important
Tuple support requires the ValueTuple type. If the .NET Framework 4.7 is not installed, you must add the NuGet package System.ValueTuple, which is available on the NuGet Gallery. Without this package, you may get a compilation error similar to, "Predefined type 'ValueTuple(Of,,,)' is not defined or imported."
In Visual Studio 2017 right click on your solution and select "Manage NuGet Packages for Solution...". In the search-box enter "valuetuple". Select "System.ValueTuple" and on the right click the check boxes of the projects where you want to install the package and click Install.
See: NuGet Package Manager UI
Also, you must declare the list variable as
Dim topic = New List(Of (String, String, String))
and add new elements with
topic.Add(("{0,-20}{1}", row("column_name"), row("data_type")))
The counter is not needed anymore.
Alternatively, you could use a list of strings and format the string with string interpolation
Dim topic = New List(Of String)
Dim filtervalues = {Nothing, Nothing, "Results", Nothing}
Using con =
New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Database.mdb")
Dim columns = con.GetSchema("columns", filtervalues)
For Each row As DataRow In columns.Rows
topic.Add($"{row("column_name"),-20}{row("data_type")}")
Next
End Using
Judging from the "{0,-20}{1}", you forgot the String.Format in
topic(counter) = String.Format("{0,-20}{1}", row("column_name"), row("data_type"))
and did not intend to use a tuple.

Getting MetaData from an image file

We have images stored on a DB and they are being used to replace an image within a Word document - that bit works perfectly, except where the replacement image is portrait and it's replacing a landscape one, so I'm trying to get the metadata to determine how the image is orientated using this function
Public Function GetImageTags(ImageFile() As Byte) As String()
Try
Dim vReturnArray() As String = Nothing
Using MS As New System.IO.MemoryStream(ImageFile)
Dim vDecoder As BitmapDecoder = BitmapDecoder.Create(MS, BitmapCreateOptions.None, BitmapCacheOption.Default)
Dim vFrame As BitmapFrame = vDecoder.Frames(0)
Dim vMetadata As BitmapMetadata = TryCast(vFrame.Metadata, BitmapMetadata)
If vMetadata IsNot Nothing And vMetadata.Keywords IsNot Nothing Then
vReturnArray = vMetadata.Keywords.ToArray()
End If
End Using
Return vReturnArray
Catch ex As Exception
EmailError(ex)
Return Nothing
End Try
End Function
...but it throws the toys out with...
This codec does not support the specified property.
at System.Windows.Media.Imaging.BitmapMetadata.GetQuery(String query)
at System.Windows.Media.Imaging.BitmapMetadata.get_Keywords()
...at BitMapMetadata.Keywords. Any idea how I can overcome this and get the keywords?
Thank you
================ UPDATE ================
It appears that the error, and I also tried...
vReturnArray = TryCast(vMetadata.GetQuery("System.Keywords"), String())
... is only returned for some images, but all that I tried returned Nothing for the String()
There is a really good EXIF class on Code Project that is easy to implement, either with a string link to the file
Dim vEXIF As New ImageEXIF(ImagePath)
Dim vOrientation As Integer = vEXIF.Orientation
or as BitMap
Dim vOrientation As Integer = 0
Using vBitmap As System.Drawing.Image = System.Drawing.Image.FromStream(New IO.MemoryStream(ImageFile))
Dim vEXIF As New ImageEXIF(vBitmap)
vOrientation = vEXIF.Orientation
End Using
It would not be difficult to add another Sub to the class for Byte(), but the above conversion is quite straightforward and the class should work with all image types.
You could use MetadataExtractor to access the image metadata.
Check for the presence of ExifDirectoryBase.TagOrientation on any of the contained Exif directories.
Something like this (sorry it's C# as I don't know VB.NET):
var orientation = ImageMetadataReader.ReadMetadata(imagePath)
.OfType<ExifSubIfdDirectory>()
.Select(d => d.GetObject(ExifDirectoryBase.TagOrientation))
.First(o => o != null);

VB.NET issue loading textboxes with data from SQL Server

I have some textboxes (using VS2010) I'm trying to populate with values from columns in a SQL Server database based on what item someone selects from a combobox. At first I was able to display the values for the first item in the combobox, but now nothing at all displays when I debug. Code:
Private Sub loadfields(sender As System.Object, e As System.EventArgs) Handles client_selection_combobox.SelectedIndexChanged
Using myconnection As New SqlConnection("connection string")
Dim loadfields As String = "SELECT company FROM ClientFileDatabase WHERE ClientFileDatabase.File_Name=#company;"
Dim loadfields_sqlcommand As New SqlCommand(loadfields, myconnection)
loadfields_sqlcommand.Parameters.Add("#company", SqlDbType.NVarChar)
loadfields_sqlcommand.Parameters("#company").Value = client_selection_combobox.SelectedIndex.ToString
Dim loadfields_dataadapter As New SqlDataAdapter
loadfields_dataadapter.SelectCommand = loadfields_sqlcommand
Dim loadfields_dataset As DataSet = New DataSet()
loadfields_dataadapter.Fill(loadfields_dataset, "ClientFileDatabase")
Dim loadfields_dataview = New DataView(loadfields_dataset.Tables("ClientFileDatabase"))
companyname_textbox.DataBindings.Clear()
companyname_textbox.DataBindings.Add("Text", loadfields_dataview, "Company")
taxid_textbox.DataBindings.Clear()
taxid_textbox.DataBindings.Add("Text", loadfields_dataview, "TaxIDNumber")
accountmanager_textbox.DataBindings.Clear()
accountmanager_textbox.DataBindings.Add("Text", loadfields_dataview, "AccountManager")
etc...
End Using
End Sub
I've also tried using the SelectedValueChanged and SelectionChangeCommitted event handlers to no avail. Also tried using a refresh after setting the databindings, didn't help. Any advice welcome, thanks!
I personally like to use datatables, I find them easier to work with. I'm sure you will have more code to check to make sure dt.rows.count > 0 before actually attempting to work with the data, but here is how I would do it.
Dim loadfields_dataadapter As New SqlDataAdapter
Dim dt As New DataTable
loadfields_dataadapter.Fill(dt)
companyname_textbox.text = dt.Rows(0).Item("Company")
taxid_textbox.text = dt.Rows(0).Item("TaxIDNumber")
accountmanager_textbox.text = dt.Rows(0).Item("AccountManager")
Also, keep in mind that NULL fields in the database can cause runtime errors, so you may have to check for those as well.

WPF Vb.net Copyto not working?

I am simply tyring to fire an event that copies a program to the Startup folder. I do not understand where I am going wrong? I keep on getting exception message. The file that is being copies is NOT in use.
Try
Dim DesktopLink As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
Dim StartupFolder As String = Environment.GetFolderPath(Environment.SpecialFolder.Startup)
Dim info As New FileInfo(StartupFolder)
info.CopyTo(DesktopLink + "\doessomething.bat")
Catch ex As Exception
MessageBox.Show("Error: Can not copy to startup folder")
End Try
Right now, you're creating a FileInfo from a folder, not a file.
This should likely be:
Dim info As New FileInfo(Path.Combine(StartupFolder, "doessomething.bat"))
info.CopyTo(Path.Combine(DesktopLink, "doessomething.bat"))
Or, even easier:
Dim source = Path.Combine(StartupFolder, "doessomething.bat")
Dim target = Path.Combine(DesktopLink, "doessomething.bat")
File.Copy(source, target)

winform's Application Settings : cannot save application settings which has been added during the run time

I having some issues with saving Application settings during the run time...
If i change the setting scope to user, it works fine, but in application scope, nothing being happened...
I've used :
Properties.Settings.Default.Save();
any ideas ?
thanks
That's because setting the scope to Application makes it read-only.
See Using Settings in C#
Application-scope settings are read only, and can only be changed at design time or by altering the .exe.config file in between application sessions. User-scope settings, however, can be written at run time, just as you would change any property value. The new value persists for the duration of the application session. You can persist changes to user settings between application sessions by calling the Settings.Save method.
You can save and read setting like all advanced programs in Registry, and that is how to do it:
Public Function GetRegistryValue(ByVal KeyName As String, Optional ByVal DefaultValue As Object = Nothing) As Object
Dim res As Object = Nothing
Try
Dim k = My.Computer.Registry.CurrentUser.OpenSubKey("Software\YourAppName", True)
If k IsNot Nothing Then
res = k.GetValue(KeyName, DefaultValue)
Else
k = My.Computer.Registry.CurrentUser.CreateSubKey("Software\YourAppName")
End If
If k IsNot Nothing Then k.Close()
Catch ' ex As Exception
'PromptMsg(ex)
End Try
Return res
End Function
Public Sub SetRegistryValue(ByVal KeyName As String, ByVal _Value As Object)
Try
Dim k = My.Computer.Registry.CurrentUser.OpenSubKey("Software\YourAppName", True)
If k IsNot Nothing Then
k.SetValue(KeyName, _Value)
Else
k = My.Computer.Registry.CurrentUser.CreateSubKey("Software\YourAppName")
k.SetValue(KeyName, _Value)
End If
If k IsNot Nothing Then k.Close()
Catch ' ex As Exception
'PromptMsg(ex)
End Try
End Sub
Or even more you can make a serializable class ([Serializable()] attrib) that contains all of your settings as properties, then save it in your app directory, with the BinaryFormatter class.
Public Sub saveBinary(ByVal c As Object, ByVal filepath As String)
Try
Using sr As Stream = File.Open(filepath, FileMode.Create)
Dim bf As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter()
bf.Serialize(sr, c)
sr.Close()
End Using
Catch ex As Exception
Throw ex
End Try
End Sub
Public Function loadBinary(ByVal path As String) As Object
Try
If File.Exists(path) Then
Using sr As Stream = File.Open(path, FileMode.Open)
Dim bf As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter()
Dim c = bf.Deserialize(sr)
sr.Close()
Return c
End Using
Else
Throw New Exception("File not found")
End If
Catch ex As Exception
Throw ex
End Try
Return Nothing
End Function
Check this post out. You just refer to the application scoped settings like this:
Properties.Settings.Default["SomeProperty"] = "Some Value";
Properties.Settings.Default.Save(); // Saves settings in application configuration file
Worked for me.

Resources