In VB, I want to dynamically create a new text file in a hard-coded file share, based on the current user logged in.
I have tested the below code, which does indeed create the test file in the specified path, but i want the test.txt file name to be dynamic, im thinking based on the environment.username class?
Dim objwriter As New System.IO.StreamWriter("\\server\path\test.txt")
objwriter.WriteLine("first line")
objwriter.WriteLine("testing")
objwriter.WriteLine("")
objwriter.Close()
Based on what I had obtained rthus far, I may have to define a variable as a string then append it to my StreamWriter write command?
Dim user_name As String = Environment.UserName
Just trying to now put the two together.. any help would be appreciative..
Thanks hackerman - this did the trick..
Dim objwriter As New System.IO.StreamWriter("\\server\path\" + user_name + ".txt")
Related
I want to create a program that auto copy text file from one folder to another folder . is it possible to make in windows form in vb.net ? if not what about in console apps ? i tried to search but i didn't find an answer for both. please help me i'm new to to this. I want to copy all the text file that is being save to c:folder1\test1.text copy to c:folder2\test1.text then test2.text,test3.text all the text file that are being put in folder1. i want to copy in folder2.
now i only have this code:
it will only copy 1 specific textfile with file name test.txt.
enter code here
My.Computer.FileSystem.CopyFile("C:\CopyTo\test.txt",
"C:\CopyHere\test.txt")
Of course! First of all we need a function that search for files.
Public Sub SearchFiles(ByVal Pattern As String, ByVal Path As String, ByVal FilesFound As ArrayList)
FilesFound.AddRange(Directory.GetFiles(Path, Pattern))
End Sub
But where we should save the list of files? We can use a Array for it. Also we should define our output and input folder
Dim files As New ArrayList
Dim inDir As String = "input path"
Dim outDir As String = "output path"
We can now call this function like this:
SearchFiles("*.txt", inDir, files)
All .txt files in the folder are now saved in our Array List. But how we can work with it? We can now work with it like this:
Try
For Each file As String In files
Dim fName As String = Path.GetFileName(file)
My.Computer.FileSystem.CopyFile(file , outDir & "\" & fName, overwrite:=False)
Next
Catch ex As Exception
Console.WriteLine(ex.ToString)
End Try
This will copy every .txt file that where found in our inDir to our outDir. If something goes wrong then you will see this in the console. Try it out and understand how it works :)
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?
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()
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)
Right now i have a line of code, in vb, that calls a text file, like this:
Dim fileReader As String
fileReader = My.Computer.FileSystem.ReadAllText("data5.txt")
data5.txt is a resource in my application, however the application doesn't run because it can't find data5.txt. I'm pretty sure there is another code for finding a .txt file in the resource that i'm overlooking, but i can't seem to figure it out. So does anyone know of a simple fix for this? or maybe another whole new line of code? Thanks in advance!
If you added the file as a resource in the Project + Properties, Resources tab, you'll get its content by using My.Resources:
Dim content As String = My.Resources.data5
Click the arrow on the Add Resource button and select Add Existing File, select your data5.txt file.
I'm assuming that the file is being compiled as an Embedded Resource.
Embedded Resources aren't files in the filesystem; that code will not work.
You need to call Assembly.GetManifestResourceStream, like this:
Dim fileText As String
Dim a As Assembly = GetType(SomeClass).Assembly
Using reader As New StreamReader(a.GetManifestResourceStream("MyNamespace.data5.txt"))
fileText = reader.ReadToEnd()
End Using
First, go to project resources (My Project --> Resources), and drag-and-drop your file, say "myfile.txt", from the file system to the resourses page.
Then:
Imports System.IO
...
Dim stream As New MemoryStream(My.Resources.myfile)
Dim reader As New StreamReader(stream)
Dim s As String = reader.ReadToEnd()