Open a file in a new instance of program - vb.net

All;
I have a bit of code I've written that opens a design blueprint when I scan a bar code. It works well enough, but I'd like to open a new instance of the design software (Solidworks) and have the print display in the new instance. Right now, no matter how many Solidworks instances I have open, the print will only open in the first instance started.
The line commented out below is the line that works, just not in the right instance. The line below that is what I'd expect to work, but it returns a 'file not found' even though the path to solidworks and the print path are both correct.
Any explanation as to why this isn't working would be much appreciated as I'm obviously very new at this...and have no idea what I'm doing.
Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
Try
Dim barcode As String = tb_barcode.Text
Dim filename As String = tb_barcode.Text
'Add File Extension to end of path
Dim ext As String = ".SLDDRW"
'Split job number from detail number in barcode textbox
barcode = Split(tb_barcode.Text, ".")(0)
filename = Split(tb_barcode.Text, ".")(1)
'- This works, just in primary instance
'System.Diagnostics.Process.Start("G:\Fixtures\" & barcode & "\Details\" & barcode & " DET " & filename & ext)
'- This does not work
System.Diagnostics.Process.Start("'C:\Program files\Solidworks Corp\Solidwork\SLDWORKS.exe' 'G:\Fixtures\" & barcode & "\Details\" & barcode & " DET " & filename & ext + "'")
Catch
MessageBox.Show("File Not Found")
End Try
End Sub

Sorry for naive approach but shouldn't there be a comma in Process.Start between 2 arguments?
Start(String, String)
Starts a process resource by specifying the name of an application and a set of command-line arguments, and associates the resource with a new Process component. docs

Why don't you use the Application.ExecutablePath.That returns the Application's path with its full name. Then your code should be
System.Diagnostics.Process.Start(Application.Executablepath, "G:\Fixtures\" & barcode & "\Details\" & barcode & " DET " & filename & ext + "'")
Also make sure that the second string argument is a valid path.

Related

Write a program to create a file that consists of only 2 fields from another text file with 6? (VB2012)

I'm using
Dim sw As IO.StreamWriter = IO.File.CreateText("Justices1.txt")
to create the file but I'm having trouble writing the code that will take only specific parts of the original "Justices.txt" file and put it into "Justices1.txt"
The first line of the original Justices file looks like this:
Henry,Baldwin,Andrew Jackson,PA,1830,1844
And I'm trying to get it to this in the file I'm creating (Justices1):
Henry Baldwin,PA
Sorry if this is a stupid question- I'm new to this.
I'd normally suggest using a StringBuilder but I figure your app won't be burning too many CPU cycles with something simple like this:
Private Sub FormatJusticesFile(filePath As String, formattedFilePath As String)
IO.File.WriteAllLines(formattedFilePath,
From line In IO.File.ReadAllLines(filePath)
Let terms = line.Split(","c)
Select "(" & IO.Path.GetFileNameWithoutExtension(filePath) & "): " & terms(0) & " " & terms(1) & "," & terms(3))
End Sub
Usage:
FormatJusticesFile("c:\Justices1.txt", "c:\Justices2.txt")

How do I display the value of a shared parameter using an Element collector in Revit?

Thanks in advance for the help. I have no idea what I'm doing wrong and it's becoming very frustrating. First, a little background...
Program: Revit MEP 2015
IDE: VS 2013 Ultimate
I have created a Shared Parameter file and added the parameters in that file to the Project Parameters. These parameters have been applied to Conduit Runs, Conduit Fittings, and Conduits.
I'm using VB.NET to populate the parameters with no issue. After the code runs, I can see the expected text applied in the elements property window. Here is the code used to populate the values:
Populate:
Dim p as Parameter = Nothing
Dim VarName as String = "Parameter Name"
Dim VarVal as String = "Parameter Value"
p = elem.LookupParameter(VarName) <-- elem is passed in to the function as an Element
If p IsNot Nothing Then
p.Set(VarVal)
End if
Here's where I run into the error. When I attempt to retrieve the value, I am able to get the parameter by the parameter's definition name, but the value is always blank. Here is the code used to retrieve...
Try
For Each e As Element In fec.OfCategory(BuiltInCategory.OST_ConduitRun)
sTemp = sTemp & "Name: " & P.Definition.Name & vbCrLf & "Value: " & P.AsString & vbCrLf & "Value As: " & P.AsValueString & vbCrLf & vbCrLf
sTemp2 = sTemp2 & "Name: " & GetParamInfo(P, doc)
Next
MessageBox.Show(sTemp)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
The message box shows all of the parameter names correctly, and for the Revit parameters it gives me a value. The Shared parameters, however, only show the parameter names, the values are always blank. Is there another way that I'm supposed to be going about this? Oddly, I'm able to see the shared parameter values if I use a reference by user selection like so...
Dim uiDoc As UIDocument = app.ActiveUIDocument
Dim Sel As Selection = uiDoc.Selection
Dim pr As Reference = Nothing
Dim doc As Document = uiDoc.Document
Dim fec As New FilteredElementCollector(doc)
Dim filter As New ElementCategoryFilter(BuiltInCategory.OST_ConduitRun)
Dim sTemp As String = "", sTemp2 As String = ""
Dim elemcol As FilteredElementCollector = fec.OfCategory(BuiltInCategory.OST_ConduitRun)
Dim e As Element = Nothing, el As Element = Nothing
Dim P As Parameter
pr = Sel.PickObject(ObjectType.Element)
e = doc.GetElement(pr)
For Each P in e.Paramters
sTemp = sTemp & "Name: " & P.Definition.Name & vbCrLf & "Value: " & P.AsString & vbCrLf & "Value As: " & P.AsValueString & vbCrLf & vbCrLf
sTemp2 = sTemp2 & "Name: " & GetParamInfo(P, doc)
Next
MessageBox.Show(sTemp)
With the method above, when the user selects the object directly, I can see the values and the names of shared parameters. How are they different?
Is there some sort of binding that I should be looking at when the value is set to begin with? Thanks in advance for everyone's help.
Regards,
Glen
Holy Bejeesus... I figured it out, but I'm not sure why the methods are that different from each other... if anyone had any insight, that'd be great.
I wanted to post the answer here, just in case anyone else is fighting with the same thing, so... you can see the method I was using to try to read the parameters above. In the method being used now there are only a couple of things that are different... 1) An element set... 2) An active view Id was added as a parameter to the FilteredElementCollector... 3) A FilteredElementIterator was implemented.
As far as I can tell it's the iterator that's making it different... can anyone explain what it's doing differently?
Below is the method that actually works...
Public Sub Execute(app As UIApplication) Implements IExternalEventHandler.Execute
Dim prompt As String = ""
Dim uiDoc As UIDocument = app.ActiveUIDocument
Dim doc As Document = uiDoc.Document
Dim ElemSet As ElementSet = app.Application.Create.NewElementSet
Dim fec As New FilteredElementCollector(doc, doc.ActiveView.Id)
Dim fec_filter As New ElementCategoryFilter(BuiltInCategory.OST_Conduit)
fec.WhereElementIsNotElementType()
fec.WherePasses(fec_filter1)
Dim fec_i As FilteredElementIterator = fec.GetElementIterator
Dim e As Element = Nothing
fec_i.Reset()
Using trans As New Transaction(doc, "Reading Conduit")
trans.Start()
While (fec_i.MoveNext)
e = TryCast(fec_i.Current, Element)
ElemSet.Insert(e)
End While
Try
For Each ee As Element In ElemSet
GetElementParameterInformation(doc, ee)
Next
Catch ex As Exception
TaskDialog.Show("ERROR", ex.Message.ToString)
End Try
trans.Commit()
End Using
End Sub
At any rate, thanks for any help that was offered. I'm sure it won't be the last time that I post here.
Regards,
Runnin

Directory.Move isn't trying the correct folder

I have made some code intended to sort all my movies on my PC into subfolders for each letter of the alphabet (e.g. "An Example" would go in a subfolder containing only movies which start with the letter "A".
The code I've written looks to me like it should work without problems, although for some reason this code:
'Declarations
System.IO.Directory.CreateDirectory("D:\Vuze Downloads\Movies\.CAMS")
System.IO.Directory.CreateDirectory("D:\Vuze Downloads\Movies\.TS")
Dim TS As String = "D:\Vuze Downloads\Movies\.TS\"
Dim CAM As String = "D:\Vuze Downloads\Movies\.CAMS\"
Dim mainFolder As New System.IO.DirectoryInfo("D:\Vuze Downloads\Movies\")
For Each f As System.IO.DirectoryInfo In mainFolder.GetDirectories()
If Not UCase(f.ToString).Contains("(CAM)") And UCase(f.ToString).Contains("(TS)") Then
System.IO.Directory.Move(f.ToString, mainFolder.Name & UCase(Left(f.Name, 1)) & "\" & f.Name)
ElseIf UCase(f.Name.ToString).Contains("(CAM)") And Not UCase(f.Name.ToString).Contains("(TS)") Then
System.IO.Directory.Move(f.ToString, CAM.ToString & UCase(Mid(f.Name, 5)) & "\" & f.Name.Substring(5))
ElseIf UCase(f.Name.ToString).Contains("(TS)") And Not UCase(f.Name.ToString).Contains("(CAM)") Then
System.IO.Directory.Move(f.ToString, TS.ToString & UCase(Mid(f.Name, 6)) & "\" & f.Name.Substring(6))
End If
Next
Keeps throwing a exception at this line:
System.IO.Directory.Move(f.ToString, CAM.ToString & UCase(Mid(f.Name, 5)) & "\" & f.Name.Substring(5))
This is the exception:
An unhandled exception of type 'System.IO.DirectoryNotFoundException' occurred in
mscorlib.dll
Additional information: Could not find a part of the path
'D:\Users\Yorrick\documents\visual studio 2012\Projects\filmsort\filmsort\bin\Debug\(CAM)The Internship'.
As indicated by the declarations above, I have no idea how or why this code is trying to access that folder.
If anyone could take a look and hopefully spot the mistake I've made, that would be very much be appreciated.
Edit:
Having fixed the above problem, I encountered a new one.
Using the code below, I now get the same exception, except this time additional information says "Could not find a part of the path." All my variables seem to be correct while debugging, so I seriously can't see why this isn't working.
Note: The commented line is something I tried, which gives me the System.IO.IOException: Cannot create a file when that file already exists.
Code:
System.IO.Directory.CreateDirectory("D:\Vuze Downloads\Movies\.CAMS")
System.IO.Directory.CreateDirectory("D:\Vuze Downloads\Movies\.TS")
Dim TS As String = "D:\Vuze Downloads\Movies\.TS\"
Dim CAM As String = "D:\Vuze Downloads\Movies\.CAMS\"
Dim mainFolder As New System.IO.DirectoryInfo("D:\Vuze Downloads\Movies\")
For Each f As System.IO.DirectoryInfo In mainFolder.GetDirectories()
If Not UCase(f.ToString).Contains("(CAM)") And UCase(f.ToString).Contains("(TS)") Then
System.IO.Directory.Move(f.FullName, mainFolder.FullName & UCase(Left(f.Name, 1)) & "\" & f.Name)
ElseIf UCase(f.Name.ToString).Contains("(CAM)") And Not UCase(f.Name.ToString).Contains("(TS)") Then
'System.IO.Directory.CreateDirectory(CAM & UCase(Mid(f.Name, 6)) & "\" & f.Name.Substring(6))
System.IO.Directory.Move(f.FullName, CAM & UCase(Mid(f.Name, 6)) & "\" & f.Name.Substring(6))
ElseIf UCase(f.Name.ToString).Contains("(TS)") And Not UCase(f.Name.ToString).Contains("(CAM)") Then
System.IO.Directory.Move(f.FullName, TS.ToString & UCase(Mid(f.Name, 5)) & "\" & f.Name.Substring(5))
End If
Next
This happens when you don't specify a full path name, like "c:\foo\bar", but a relative path name, like "bar". Relative path names are turned into full ones by prepending Environment.CurrentDirectory. Which by default is the build directory of your project.
This happened because you used DirectoryInfo.Name. Which is "bar" for a directory whose path is c:\foo\bar. You must use the FullName property instead.
I just rewrote it a bit, i think that should suffice for your purpose.
System.IO.Directory.CreateDirectory("D:\Vuze Downloads\Movies\.CAMS")
System.IO.Directory.CreateDirectory("D:\Vuze Downloads\Movies\.TS")
Dim TS As String = "D:\Vuze Downloads\Movies\.TS\"
Dim CAM As String = "D:\Vuze Downloads\Movies\.CAMS\"
Dim mainFolder As New System.IO.DirectoryInfo("D:\Vuze Downloads\Movies\")
For Each f As System.IO.DirectoryInfo In mainFolder.GetDirectories()
If f.Name.ToUpper.Contains("(TS)") Then
System.IO.Directory.Move(f.FullName, System.IO.Path.Combine(TS, f.Name))
ElseIf f.Name.ToUpper.Contains("(CAM)") Then
System.IO.Directory.Move(f.FullName, System.IO.Path.Combine(CAM, f.Name))
End If
Next

Open, Launch or Show a file for the user to read or write in vb.net

It sounds very simple but I have searched and cannot seem to find a way to open a log file which the user just created from my windows form app. The file exits I just want to open it after it is created.
I have a Dim path As String = TextBox1.Text and once the user names and clicks ok on the savefiledialog I have a msgbox that says "Done" and when you hit OK I have tried this
FileOpen(FreeFile, path, OpenMode.Input) but nothing happens. I just want it to open the log and show it to the user so they can edit or save it again or anything.
This is where I got the above code.
http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.filesystem.fileopen.aspx
Searching is difficult because everyone is trying to "Open" a file and process it during runtime. I am just trying to Show a file by Launching it like someone just double clicked it.
Here is the entire Export Button click Sub. It basically writes listbox items to file.
Private Sub btnExport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExport.Click
Dim sfd As New SaveFileDialog
Dim path As String = TextBox1.Text
Dim arypath() As String = Split(TextBox1.Text, "\")
Dim pathDate As String
Dim foldername As String
foldername = arypath(arypath.Length - 1)
pathDate = Now.ToString("yyyy-MM-dd") & "_" & Now.ToString("hh;mm")
sfd.FileName = "FileScannerResults " & Chr(39) & foldername & Chr(39) & " " & pathDate
sfd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal)
sfd.Filter = "Text files (*.txt)|*.txt|CSV Files (*.csv)|*.csv"
sfd.ShowDialog()
path = sfd.FileName
Using SW As New IO.StreamWriter(path)
If CkbxFolder.Checked = True Then
SW.WriteLine("Folders")
For Each itm As String In ListBox1.Items
SW.WriteLine(itm)
Next
End If
If CkbxFiles.Checked = True Then
SW.WriteLine("Files")
For Each itm As String In ListBox2.Items
SW.WriteLine(itm)
Next
End If
End Using
MsgBox("Done...")
FileOpen(FreeFile, path, OpenMode.Input) 'Why can't I open a file for you...
End Sub
Do not use the old VB6 methods. They are still here for compatibility reason, the new code should use the more powerful methods in the System.IO namespace.
However, as said in comments, FileOpen don't show anything for you, just opens the file
You coud write
Using sr = new StreamReader(path)
Dim line = sr.ReadLine()
if !string.IsNullOrEmpty(line) Then
textBoxForLog.AppendText(line)
End If
End Using
or simply (if the file is not too big)
Dim myLogText = File.ReadAllText(path)
textBoxForLog.Text = myLogText
As alternative, you could ask the operating system to run the program associated with the file extension and show the file for you
Process.Start(path)
To get the same behavior as if the user double-clicked it, just use System.Diagnostics.Process, and pass the filename to it's Start method:
Process.Start(path)
This will open the file using whatever the default application is for that filename based on its extension, just like Explorer does when you double-click it.

How do I customize the auto commenting text in Visual Studio?

When I type the trigger the auto comment feature in Visual Studio (by typing "'''" or "///"), most of the XML commenting details show up that I like. However, I typically add the history tag to the documentation so I can track and changes that are made to the method over time.
Is there any way I can customize the auto commenting feature so that it will add the history tag, and potentially some generic Name - Date - Change placeholder text?
I'd suggest using GhostDoc. It generates very smart comments using /// based on your method names and parameters. Also, it is fully customizable.
I think that you could use a tool as dgarcia said but try to chose one that makes the version control insetad, Personally I'm not a huge fan of keep the "history" or track of the project using comments in the code.
If you like that way you could create your own customized version of the snippet, this is easier if you use a tool like Snippy
Copy this file to your
My Documents\Visual Studio 2005\Code Snippets[Language]\My Code Snippets\
Just be carefull to change the file if you gonna use it in VB.NET
Hope this help
Just as followup to the comment to Olivier. Here is a copy of the macro now, look for the '' Do History section to see where I inserted code.
''// InsertDocComments goes through the current document using the VS Code Model
''// to add documentation style comments to each function.
''
Sub InsertDocComments()
Dim projectItem As ProjectItem
Dim fileCodeModel As FileCodeModel
Dim codeElement As CodeElement
Dim codeElementType As CodeType
Dim editPoint As EditPoint
Dim commentStart As String
projectItem = DTE.ActiveDocument.ProjectItem
fileCodeModel = projectItem.FileCodeModel
codeElement = fileCodeModel.CodeElements.Item(1)
''// For the sample, don't bother recursively descending all code like
''// the OutlineCode sample does. Just get a first CodeType in the
''// file.
If (TypeOf codeElement Is CodeNamespace) Then
codeElement = codeElement.members.item(1)
End If
If (TypeOf codeElement Is CodeType) Then
codeElementType = CType(codeElement, CodeType)
Else
Throw New Exception("Didn't find a type definition as first thing in file or find a namespace as the first thing with a type inside the namespace.")
End If
editPoint = codeElementType.GetStartPoint(vsCMPart.vsCMPartHeader).CreateEditPoint()
''// Make doc comment start.
commentStart = LineOrientedCommentStart()
If (commentStart.Length = 2) Then
commentStart = commentStart & commentStart.Chars(1) & " "
ElseIf (commentStart.Length = 1) Then
commentStart = commentStart & commentStart.Chars(0) & commentStart.Chars(0) & " "
End If
''// Make this atomically undo'able. Use Try...Finally to ensure Undo
''// Context is close.
Try
DTE.UndoContext.Open("Insert Doc Comments")
''// Iterate over code elements emitting doc comments for functions.
For Each codeElement In codeElementType.Members
If (codeElement.Kind = vsCMElement.vsCMElementFunction) Then
''// Get Params.
Dim parameters As CodeElements
Dim codeFunction As CodeFunction
Dim codeElement2 As CodeElement
Dim codeParameter As CodeParameter
codeFunction = codeElement
editPoint.MoveToPoint(codeFunction.GetStartPoint(vsCMPart.vsCMPartHeader))
''//editPoint.LineUp()
parameters = codeFunction.Parameters
''// Do comment.
editPoint.Insert(Microsoft.VisualBasic.Constants.vbCrLf)
editPoint.LineUp()
editPoint.Insert(Microsoft.VisualBasic.Constants.vbTab & commentStart & "<summary>")
editPoint.Insert(Microsoft.VisualBasic.Constants.vbCrLf)
editPoint.Insert(Microsoft.VisualBasic.Constants.vbTab & commentStart & "Summary of " & codeElement.Name & ".")
editPoint.Insert(Microsoft.VisualBasic.Constants.vbCrLf)
editPoint.Insert(Microsoft.VisualBasic.Constants.vbTab & commentStart & "</summary>")
editPoint.Insert(Microsoft.VisualBasic.Constants.vbCrLf)
editPoint.Insert(Microsoft.VisualBasic.Constants.vbTab & commentStart)
For Each codeElement2 In parameters
codeParameter = codeElement2
editPoint.Insert("<param name=" & codeParameter.Name & "></param>")
editPoint.Insert(Microsoft.VisualBasic.Constants.vbCrLf)
editPoint.Insert(Microsoft.VisualBasic.Constants.vbTab & commentStart)
Next ''//param
''// Do history tag.
editPoint.Insert(Microsoft.VisualBasic.Constants.vbCrLf)
editPoint.LineUp()
editPoint.Insert(Microsoft.VisualBasic.Constants.vbTab & commentStart & "<history>")
editPoint.Insert(Microsoft.VisualBasic.Constants.vbCrLf)
editPoint.Insert(Microsoft.VisualBasic.Constants.vbTab & commentStart & "Name MM/DD/YYYY [Created]")
editPoint.Insert(Microsoft.VisualBasic.Constants.vbCrLf)
editPoint.Insert(Microsoft.VisualBasic.Constants.vbTab & commentStart & "</history>")
editPoint.Insert(Microsoft.VisualBasic.Constants.vbCrLf)
editPoint.Insert(Microsoft.VisualBasic.Constants.vbTab & commentStart)
End If ''//we have a function
Next ''//code elt member
Finally
DTE.UndoContext.Close()
End Try
End Sub
For some reason, after a save, rebuild, and a restart of Visual Studio, I'm not getting the history tag. Can anybody see something here I'm missing?
vb uses a xml file to load the defults. It is VBXMLDoc.xml and it depends on what version you are running as to the location of the file.