Inserting into another seperate line with Visual Basic - vb.net

Insert Input into New Line
'Set the ini Path'
Dim iniPath As String = "settings.ini"
'Set Text into ini File
Dim settings As String = TextBox2.Text
'Appends New Line to the settings file
My.Computer.FileSystem.WriteAllText(iniPath, settings, True)
At this point I want the settings to break into a new line when it's put into the settings file. For instance; if I put Setting1 into the file and then I put setting 2 into the file at another time it's going to produce
Setting1Setting 2
Where I want it to produce
Setting1
Setting 2
Does anybody know how I can do this in Visual Basic 2010?
A little bit of clarification:
So In the project you have the option to add an application configuration file which then shows itself as settings.config. I want this to be able to be edited by a textbox for instance;
TextBox2.Text=ServerIP.Text
So that when they click on TextBox2.Text and enter 192.168.1.12 and then click save it will write 192.168.1.12 into their configuration as a saved IP and they can pull it up at a later time and switch between.
Hopefully that makes a little more sense to anyone who might have any ideas.

You're going to need to find a way to split up each setting from the TextBox2.Text and then add a VbCrLf in between them. Try this to see how it works:
My.Computer.FileSystem.WriteAllText(iniPath, "Setting1" & VbCrLf & "Setting 2", True)

Related

Automaticaly open xls file on program start?

To provide our users to edit excel files without ms excel, we have made a simple app with visual studio 2012 and devexpress Spreadsheet module.
It is very simple to open excel file and use it.
But now only one excel file is being used (with multiple sheets), and I would like the file being used to be opened always on startup.
If I add the path and filename to command line arguments, noting happens...
Using devexpress components is very different then vanilla code for me, I am a complete beginner here, so I have no idea how to continue - can someone, please point me in the right direction?
I have made a procedure, to open the file dialog and load the file - I don't know how to "pass" it to devexpress, so the file actually loads to workbook.
Private Sub OpenXls()
Dim ofd As OpenFileDialog = New OpenFileDialog
ofd.DefaultExt = "xls"
ofd.FileName = "FILE"
ofd.InitialDirectory = "C:\ref_files"
ofd.Title = "Select file"
End Sub
As you have pointed out - using the dialog is not the right way.
After some googling I have find out that this should be a better way:
Dim workbook As New Workbook
workbook.LoadDocument("C:\ref_files\file.xls", DocumentFormat.xls)
I do not get any error, but the file is also not shown...
Do I have to display the document manually after loading?
If you're using openfiledialogue to open file then you must use command to load the specific file at Form.Load event.
The backslash char in ofd.InitialDirectory = "C:\ref_files"escapes the letter R to carrige return.
Change this line to ofd.InitialDirectory = "C:\\ref_files" (add another backslash).

UnauthorizedAccessException with File.AppendAllText in VB.NET

I have recently started getting System.UnauthorizedAccessException errors when using File.AppendAllText to write to a shared drive on the network. I think there were some changes to the network when this happened. The code in my application hasn't changed.
I have asked our IT dept to grant me full permission to the folder. I can see I have permissions for Modify, Read & Execute, Read, Write under my username if I navigate to the file and look at the Security tab under properties. I am also part of a group with read, write and modify permissions to the folder.
This works without error in the same folder:
File.WriteAllText(myFile, myText)
This generates a System.UnauthorizedAccessException error when it reaches the AppendallText:
If File.Exists(myFile) = False Then
' Create a file to write to.
Dim createText As String = logTime & " " & report_data
File.WriteAllText(myFile, createText)
Else
Dim appendText As String = logTime & " " & report_data
File.AppendAllText(myFile, appendText)
End If
I have tried deleting the file and creating it again, that made no difference.
I tried File.SetAttributes(myFile, FileAttributes.Normal)
The IT dept can't see what the problem is.
I can manually open, change and modify the file. The problem only arises if I am trying to do this programmatically.
Is there a different 'user' which tries to modify files? Could the file be open somehow, or would that generate a different error?
I'm using VB.NET 2012, .net framework 4.5, Windows 8.1
The network changes were the problem. It doesn't seem possible to resolve this as it is. Instead I made a copy of the text data, append my new text to that, delete the file, and save the updated text to a new file.

Appending lines to a text file on an FTP server

I am making a simple hangman game as a school project, I thought it would be a good idea if players were able to submit words for other players to attempt to guess. I am required to use Visual Basic 2008 for this project.
I was wondering if there was any way to append a line to an existing text file on an FTP server. And if possible a way to edit existing lines.
For example I want to be able to submit lines in the format of:
Word;Submitter;CorrectGuesses;IncorrectGuesses
And also be able to check if the file contains a line where Word = Word And Submitter = Submitter, so that I can add 1 to either the number of correct or incorrect guesses of the word.
Thank you in advance for any help you can give me.
File unavailable (e.g., file not found, no access).
This happens if the file not exists yet.
So, let's create the file if it's not existing:
Dim str As String = "string to upload"
Dim vmx As String = ""
Try
vmx = wc.DownloadString("ftp://your-url/file.txt") & vbNewLine
Catch
wc.UploadString("ftp://your-url/file.txt", "")
vmx = ""
End Try
wc.UploadString("ftp://your-url/file.txt", vmx & str)
Add line to existing File
Add this to the top:
Imports System.Net
Connect to the FTP server:
Dim wc As New WebClient
wc.Credientals = New NetworkCrediental("FTP-USERNAME", "FTP-PASSWORD")
Upload string:
Dim str As String = "upload string"
wc.UploadString("ftp://.....com/datei.txt", wc.DownloadString("ftp://.....com/datei.txt" & vbNewLine & str)

Opening a file using impersonation

I have been searching the web looking for a way to open a WORD file from a secure network folder by impersonating a user who has access. The closest I've come to finding the answer was this from 2 years ago:
Impersonating in .net (C#) & opening a file via Process.start
Here is the code that I am using. When I set the arguments = LocalFile_Test, everything works perfectly because the user is accessing the local c:\ that is has access to. But when I set arguments = RemoteFile_Test, Word opens up a blank document which is the same effect as if I put garbage in the arguments. So it appears that it cannot find the file even though when I login with the user/domain/password that I specify in the properties below, I can find that exact file name and it is not empty. Does anything jump out at you right away? I appreciate your time.
Dim LocalFile_Test As String = "C:\New.docx"
Dim RemoteFile_Test As String = "\\Server1\Apps\File\New.docx"
Dim MyStartInfo As New System.Diagnostics.ProcessStartInfo
MyStartInfo.FileName = "C:\Program Files\Microsoft Office\Office12\WINWORD.exe "
MyStartInfo.Arguments = LocalFile_Test
MyStartInfo.LoadUserProfile = True
MyStartInfo.UseShellExecute = False
MyStartInfo.UserName = "specialuser"
MyStartInfo.Domain = "mydomainname"
MyStartInfo.Password = New System.Security.SecureString()
MyStartInfo.Password.AppendChar("p"c)
MyStartInfo.Password.AppendChar("a"c)
MyStartInfo.Password.AppendChar("s"c)
MyStartInfo.Password.AppendChar("s"c)
Process.Start(MyStartInfo)
My understanding is that you are trying to get a password protected file from a server, and when you do process start, it just opens up a blank word doc. I think the error is how you are trying to get the file, I think you have to map the actual physical path of the file on the server, like
System.Web.HttpContext.Current.Server.MapPath("\\Server1\Apps\File\New.docx")
From there, I am fairly certain, you need to create network credentials for the user like
System.Net.NetworkCredential=New NetworkCredential(userName:=, password:=)
Finally, once that is done, you can either write the file, or transmit the file like so...
System.Web.HttpContext.Current.Response.TransmitFile(file name)
System.Web.HttpContext.Current.Response.WriteFile(file name)
Then,once you get the file, you can try to open it with process start.
Hope that helps, let me know if what I said doesn't work.

the process cannot access the file because it is being used by another process in vb.net

help me.. i'm new in visual basic....
when i'm running the update it shows the error
The process cannot access the file 'C:\Documents and Settings\Macky\My Documents\Visual Studio 2008\Projects\Marcelo 2.2.3\Marcelo\bin\Debug\Students\MIC953867.jpg' because it is being used by another process.
my code is this
Public Sub copingfile()
If inFileName = Nothing Then
studpic.Image = Nothing
Else
outFileName = inFileName
pos = inFileName.LastIndexOf(".")
If (pos > 0) Then
outFileName = outFileName.Substring(0, pos)
End If
outFileName += ".jpg"
str = Application.StartupPath & "\Students\"
saveJPEGFile.FileName = str & StudID.Text & ".jpg" '& outFileName
fil1.Copy(inFileName, saveJPEGFile.FileName, True) 'the error shows here...
outFileName = saveJPEGFile.FileName()
End If
End Sub
I can save new student information with picture.. but when it comes in updating the picture these codes didn't work......
fil1.Copy(inFileName, saveJPEGFile.FileName, True)
You're attempting overwrite a file that's open or being used. If the file is open in a viewer/editor, then it can't be copied over. Either you opened it manually, or did so through code and it's still "attached" to something running.
If it's not open in a window, try stopping your code and deleting that file manually. If you can, it's pretty obvious something in code is still using it when you get to the line that errored. You'll need to figure out where that file is still being used (Open stream somewhere? Open in VS, itself?), as it doesn't appear to be in the code you provided.
You are going to need to show more code, you are using variables not in your code listing. Plus you do not show the code that originally saves your image.
But here is my guess...are you sure you closed the file when you saved it for the first time? You cannot generally copy to, or from, a file that is open.
(Files can be opened as shared, but I don't think you are doing that).
Post more code if you get a chance.