Appending lines to a text file on an FTP server - vb.net

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)

Related

How to use String name in StringBuilder.AppendLine()?

I'm trying to use string name in appendline() function. The string named "programFiles" gets the program files location correctly, but I need to include it in append line (to write the path to the file) Tried this,
Dim programFiles As String = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)
Dim sb As New StringBuilder()
sb.AppendLine("Your " & programFiles & "is working properly.")
File.WriteAllText(Environment.GetEnvironmentVariable("appdata") & "\mytext.txt", sb.ToString())
But it doesn't write to a file. What is the correct way to do that ?
When I tried your code on my machine, it worked as I would have expected, the file c:\users\[myusername]\Appdata\Roaming\mytext.txt was created and contains one line
"Your C:\Program Files (x86)is working properly."
so obviously your VB is correct. Maybe you're looking in the wrong subdirectory?

How to delete all text in a text file on ftp server without deleting file itself in vb.net

Hi I am doing a chat orientated program in vb.net. I have everything working nicely but there is a feature I am trying to add where if the user types /clear it will then delete the chat text from the file, (the chat is an ftp connection and it writes to a text file)
i'm hoping to only delete text from the file not the whole file itself, i have tried appending but that as you probably know already just writes extra text to the file.
If anyone can help it would be great cheers :D
The way to go should be upload a new empty file overwriting the one on the FTP, with the same filename and path.
Dim wcFTP As New WebClient()
'wcFTP.Proxy = ...
wcFTP.BaseAddress = "ftp://foobar.domain.com"
wcFTP.Credentials = New NetworkCredential("user", "pass")
Dim sFilePath As String '= ...
Dim sFtpPath As String = wcFTP.BaseAddress & sFolder & Path.GetFileName(sFilePath)
wcFTP.UploadFile(sFtpPath, sFilePath)
Where sFilePath is the path of the empty file with the same name as the one in the server.
To get an empty file you can donwload and empty the file on the ftp, or simply create a new file with the same name and no data. For example:
Dim sFilePath As String = "C:\Folder\fileName.xml"
File.Create(sFilePath).Dispose()

Inserting into another seperate line with Visual Basic

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)

Creating a directory named with user's text in Visual Basic

I'm fairly new to programming in general. I'm working on a simple app that could combine a couple of functions to automate or simplify some of the things I do at work. One of the functions I'm trying to build is to be able to create a folder. Now I have found an article on that on Microsoft's msdn resource and it's child's play. But the instructions there only show how to create a folder with a predefined name in the code. What I'd rather want is to have a textbox where I input the folder's name and the directory is named with that input. The msdn code looks like this:
My.Computer.FileSystem.CreateDirectory _
("C:\vb\")
I understand I should now add:
Dim txt As String
txt = TextBox1.Text
But what next? How do I tell VB to use as directory name the input "txt"?
Try this:
Dim txt As String
txt = TextBox1.Text
My.Computer.FileSystem.CreateDirectory("C:\" & txt & "\")
Using & is simple and often fine for most purposes, but for your two (including what you've added as a comment) concatenation examples there are other methods:
For pathname manipulation look at System.IO.Path:
My.Computer.FileSystem.CreateDirectory(Path.Combine("C:\", txt))
For (complex) string "formatting", consider String.Format:
Dim menu As String = String.Format("Today's main dish is {0}.", TextBox2.Text)

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.