Writing to text file with StreamWriter. File used by another process - visual-studio-2010

The answer to this is probably really simple and basic, however im having trouble when writing to a file as I am greeted with this error message.
"IO Exception was unhandled."
"The process cannot access the file 'C:\Users\Steven\Documents\Visual Studio 2010\Projects\SmithsSocialCare\Login Information.txt' because it is being used by another process."
This is the code in question.
Dim FileW As System.IO.StreamWriter
Dim Username As String
Dim Password As String
FileW = My.Computer.FileSystem.OpenTextFileWriter("C:\Users\Steven\Documents\Visual Studio 2010\Projects\SmithsSocialCare\Login Information.txt", True)
If TxtPass.Text = TxtPass2.Text Then
Username = TxtUser.Text
Password = TxtPass.Text
FileW.WriteLine(Username & " " & Password)
FileW.Close()
End If
Cheers in advance, Steven.

Related

OS X Excel 2011 VBA Save File Name

I need to write a script to save a worksheet with to a predetermined location and the save name is filled from values within the worksheet. I can get it to save in the proper location, but the file name returns a combination of FATPMetiFolderPath and FATPMetiPath (\Volumes\MFS1\Groups\METI...\METIman\MMP0123 - FATP.xlsm). I can do this just fine with Windows Excel VBA, but I have never used a Mac before. I am programming on a PC, but it needs to be able to be saved properly if used on a Mac.
Sub saveFATPMMMac()
'Saves copy for access for everyone
Dim FATPMetiPath As String
Dim FATPMetiFolderPath As String
FATPMetiFolderPath = "\Volumes\MFS1\Groups\METI\Quality Control\Function and Acceptance Test Documents\METIman\"
'FATPMetiFolderPath = "C:\Users\gzapantis\Desktop\"
FATPMetiPath = FATPMetiFolderPath & _
Sheets("Failure Report").Range("FailReportSN").Text & " - FATP " & ".xlsm"
ThisWorkbook.SaveAs Filename:=FATPMetiPath
End Sub
I have solved the problem. It saves it with the correct file name and in the correct location.
Sub saveFATPMMMac()
'Saves copy for access for everyone
Dim FATPMetiPath As String
Dim FATPMetiFolderPath As String
If Application.PathSeparator = ":" Then
FATPMetiFolderPath = "Volumes:MFS1:Groups:METI:Quality Control:Function and Acceptance Test Documents:METIman:"
Else
FATPMetiFolderPath = "F:\Groups\METI\Quality Control\Function and Acceptance Test Documents\METIman\"
End If
FATPMetiPath = FATPMetiFolderPath & _
Sheets("Failure Report").Range("FailReportSN").Text & " - FATP.xlsm"
ThisWorkbook.SaveAs Filename:=FATPMetiPath
End Sub
Thank you for pointing me in the right direction.

WMI Query and VB6 error

I'm developing a routine using VB6, you must refresh the screen each second, searching for a specific process running on the system.
I'm using WMI to query processes. But sometimes I get a return "not found", and the application closes. the question is, I can filter the user inside the WMI query using a join or something?
below is my source code
Dim UserName As String
Dim UserDomain As String
Dim proc_query As String
Dim proc_results As Object
UserName = Environ("USERNAME")
proc_query = "select * from Win32_Process where Name = 'Exe1234.EXE'"
Set proc_results = GetObject("Winmgmts:").ExecQuery(proc_query)
For Each info In proc_results
colProperties = info.GetOwner(strNameOfUser, strUserDomain)
If strNameOfUser = UserName Then
// Call a external .exe
Unload Me: Exit For
End If
Next info

How can I delete a user and all files associated with them using VB6 and CMD?

I am making a small programe in Visual Studio 2012 and am integrating a few CMD prompts.
I know how to execute CMD and delete a user from within VB6.
Shell("net user """ + UserName.Text + """ /del")
I need to basically find the user's directory and have it returned as a variable to execute Shell("rd /s /q """ + DirectoryPath + """")
Unless there's a way to delete files along with the user. So maybe there's an extension on net user MyUsername /del
I've looked around on Google and come up short so any help would be great
Thanks in advance:)
i think the best way to describe what Josh Mason wants is to put in to you in a different way
so using the command
net user %username% /profile:{Full Path}
sets the profile path , is there a way of getting the profile path that is already set , so maybe using something like outputting the path to a notepad file then getting that into VB
i know you can use
net user %username% >> C:\currecutuser.txt
that would put the path into a text put along with all the other info , is there a way of just selecting the path and getting it into Vb somehow
Maybe you Should do it like that
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' get desktop path for current user
TextBox3.Text = My.Computer.FileSystem.SpecialDirectories.Desktop.ToString
'strips "\Desktop"
Dim path As String = TextBox3.Text.Replace("\Desktop", "")
TextBox3.Text = path
End Sub
then you could do Process.Start(Textbox3.Text)
to get that path
or do you want to delete that folder?
New Type i hope it make that thing work ;)
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
' get desktop path for current user
TextBox1.Text = My.Computer.FileSystem.SpecialDirectories.Desktop.ToString
'input for username to delete
Dim usertodel As String = InputBox("Type Username:")
'strips "\Desktop" & curr user name
Dim user As String = System.Environment.UserName
Dim path1 As String = TextBox1.Text.Replace("\" & user & "\Desktop", "") '= C:\Users\
TextBox1.Text = path1 & "\" & usertodel '= C:\Users\ + input box for user
End Sub
it worked for me :)

Error - cannot encrypt notesdocument when instantiated by notesuidocument

I get the error "cannot encrypt notesdocument when instantiated by notesuidocument" at the line cjDoc.Encrypt. Can someone tell me how to fix/why it is happening.
Dim currDb as NotesDatabase
Set currDb=session.CurrentDatabase
Set cjDoc = currDb.GetDocumentByUNID(Trim(Source.Document.CJ_UNID(0)))
If Not cjDoc Is Nothing Then
Dim parleyRtItem As NotesRichTextItem
Set parleyRtItem = New NotesRichTextItem(cjDoc,CJ_PARLEY_LINK)
Call parleyRtItem.AppendDocLink(parleyDoc,"Credit Jacket Parley")
cjDoc.ParleyUNID = Source.Document.parleyUNID
'cjDoc.parleyCreation = "Parley document created " & Cstr(Today) & " : "
cjDoc.parleyCreation = "Parley document created " & Cstr(Today) & " "
cjDoc.Encrypt
Call cjDoc.Save(True,True)
End If
The error suggests that you cannot encrypt it while your uidocument is open.
Dit you try closing the uidocument before encrypting?
I have seen similar issues worked around by closing the UI doc, and putting the encrypt code in the Terminate event on the UI doc. Saves working out how to fire the agent.
by the time Terminate runs, the ui doc handle is dropped from memory (which is exactly what you need to happen, so Notes doenst give you the same error) so you have to do the 'make note of UNID and get the backed document afresh" bit that tvdpol suggests.
In queryClose of UI doc, set a temp var with the UNID ..
Dim s as new notessession
s.setEnvironmentVar("TempUNID",source.document.universalID)
In Terminate event of uidoc, get the UNID and get the original doc, meaning just the backend doc- UI doc is no longer in memory ..
Dim s as new notessession
Dim doc as notesdocument
Dim sUNID as string
sUNID = s.getEnviromnentString"TempUNID"
set doc=ds.currentdatabase.getDocumentByUNID(sUNID)
' do encryption
'
doc.save(false,false)

Example VB6 code for Siemens OPC Client?

I am trying to update an ancient VB6 project to enable communication with a remote OPC Server. I have installed the Siemens toolkit but I am unable to find any useful documentation on how to use it with VB6. (Works with C#)
The application is very simple. I just need to connect to the remote server and write/read single addresses.
I found the DatCon OCX control which I assume handles the communication but all the ServerName values I tried to enter by hand did not work.
Can anyone help?
Add a reference to the DLL or OCX (the seimens toolkit) to your VB6 project and then use the object browser to browse around the exposed objects. You can often times figure out what you need just be doing that.
The C# docs should also provide a wealth of info. If the library is a COM library, you'll use it essentially the same way from VB6.
Since posting, I did make some progress. The following example helped me to get going.
http://support.automation.siemens.com/WW/llisapi.dll?func=cslib.csinfo&objId=25229521&load=treecontent&lang=en&siteid=cseus&aktprim=0&objaction=csview&extranet=standard&viewreg=WW
Here is my current code. It's not much - just makes contact with the server and tries to write a value. I didn't get any further. I started getting COM errors and assumed the installation was bad (I had had problems installing) so I decided to reinstall. It didn't work. Installation was impossible. Waiting for an upgrade from Siemens.
'
' OPC Communication
'
' Paul Ramsden 24.11.2011
'
'
Option Explicit
Option Base 1
Public MyOpcServer As OPCServer
Public ServerHandle As Variant
Private ServerName As String
Private ServerNode As String
Private TestGroup As OPCGroup
Private MyOpcItem As OPCItem
Private IsInitialised As Boolean
Public Sub InitialiseOPC()
On Error GoTo ProcError
IsInitialised = False
Set MyOpcServer = New OPCServer
ServerNode = "xyz.abc.10.101"
ServerName = "OPC.SimaticNET.1"
Dim LocalServers
LocalServers = MyOpcServer.GetOPCServers(ServerNode)
Dim tmp
ServerHandle = ""
For Each tmp In LocalServers
If CStr(tmp) = ServerName Then
Call MyOpcServer.Connect(tmp)
MsgBox MyOpcServer.ServerNode & vbCr & MyOpcServer.ServerName & vbCr & MyOpcServer.ServerState
ServerHandle = tmp
Set TestGroup = MyOpcServer.OPCGroups.Add("TestGroup")
Exit For
End If
Next
If ServerHandle = "" Then
MsgBox "Could not find server " & ServerName & " on " & ServerNode
Else
IsInitialised = True
End If
ProcExit:
Exit Sub
ProcError:
MsgBox Err.Description
Resume ProcExit
End Sub
Private Sub ClearGroup()
Dim handles() As Long
Dim errors() As Long
Call TestGroup.OPCItems.Remove(TestGroup.OPCItems.Count, handles, errors)
End Sub
Public Sub WriteOPC(address As String, value As String)
On Error GoTo ProcError
Call ClearGroup
Set MyOpcItem = TestGroup.OPCItems.AddItem(address, 2011)
MyOpcItem.Write (value)
Exit Sub
ProcError:
MsgBox "Write error! " & Err.Description
End Sub
Public Function ReadOPC(address As String) As String
On Error GoTo ProcError
Call ClearGroup
Set MyOpcItem = TestGroup.OPCItems.AddItem(address, 2011)
Dim value As String
ReadOPC = MyOpcItem.Read
ProcError:
MsgBox "Read error! " & Err.Description
End Function
Public Sub TestOPC()
InitialiseOPC
WriteOPC "SIMATIC 300(1).CPU 315-2 DP.Q0_0TestAusgang1", "1"
End Sub

Resources