.NET Framework - Template for Command-Line utility
Asked By Phil Hellmuth
29-Oct-08 01:01 PM
I'm fairly new to VS/VB.net. I want to create a command line utility
that uses two parameters, reads a file (first parm), and writes to
another file (2nd parm). Which VS/VB.net template should I use to get
started? Thanks in advance.
FileAccess.Write
(1)
FileMode.Create
(1)
FileStream
(1)
FileAccess
(1)
FileMode
(1)
VB
(1)
Application
(1)
FileExists
(1)
kimiraikkonen replied...
Start New Project, Choose "Console Application".
BTW, is all your intention to copy files using like
System.IO.File.Copy?
Thanks,
Onur Guzel
Tom Shelton replied...
You'll want to create a Console application. Your main should probably
look like this:
Public Function Main (ByVal args() As String) As Integer
' args contains all the arguments passed to your application
' assuming that the arguments are correct (you'll want to put
' in validation/parsing code here...
Dim inputFile As String = args(0)
Dim outputFile As String = args(1)
' do cool stuff
' You will most likely want to return an exit code
' 0 means no error, non-zero means a problem - you might
' want to have different codes for different problems, but
' that's up to you. You don't have to do this at all :)
' you could make Main a sub instead of a function, but
' most command line apps return an exit code to the OS -
' so it's a good idea to do the same.
Return AnExitCode
End Function
HTH
--
Tom Shelton
Phil Hellmuth replied...
Thanks for your response. This really helps. Unfortunately, I'm stuck
with what I'm trying to do next. I want to see if the files specified
in the arguments exist, using the FileExists function. However, I don't
know what reference to use to import System.File.IO (as I said, I'm new
to this). Next, I want to read each byte in the input file and write it
to the output file if it's less than 128 (ascii). Is this relatively
simple?
Dennis replied...
Have you tried the Help for FileExists? Often there will be a code
snippet to show how it works. Also try googling for sample code.
--
Dennis
Tom Shelton replied...

Actually, it is :) Here is some air-code, this untested, and is infact not
likely to work the first try... I might have my indexing off or some minor
syntax issues, but it should be enough to get you started :)
Option Explict On
Option Strict On
Imports System
Imports System.IO
Public Function Main (ByVal args() As String) As Integer
' args contains all the arguments passed to your application
' assuming that the arguments are correct (you'll want to put
' in validation/parsing code here...
Dim inputFile As String = args(0)
Dim outputFile As String = args(1)
Dim anExiteCode As Integer = 0
Dim inBuffer() As Byte = new Byte(2048)
Dim outBuffer() As Byte = new Byte(2048)
Dim bytesRead As Integer = 0
dim bytesWritten = 0
Try
Using inFile As New FileStream(inputFile, FileMode.Open, FileAccess.Read), _
outFile As New FileStream(ouputFile, FileMode.Create, FileAccess.Write)
bytesRead = inFile.Read(inBuffer, 0, inBuffer.Length)
while bytesRead > 0
for i as integer = 0 to bytesRead - 1
if inbuffer(i) > 128 then
outBuffer(bytesWritten) = inbuffer(i)
bytesWritten += 1
end if
next i
outFile.Write(outBuffer, 0, bytesWritten)
bytesRead = inFile.Read(inBuffer, 0, inBuffer.Length)
end while
End Using
Catch ex As Exception
anExitCode = 1
End Catch
' You will most likely want to return an exit code
' 0 means no error, non-zero means a problem - you might
' want to have different codes for different problems, but
' that's up to you. You don't have to do this at all :)
' you could make Main a sub instead of a function, but
' most command line apps return an exit code to the OS -
' so it's a good idea to do the same.
Return anExitCode
End Function
Anyway, hth
--
Tom Shelton
Tom Shelton replied...
Actually, i did not notice your question about references :) You do not need to
add any additional references - just add the Imports System.IO.
--
Tom Shelton
button1_Click(object sender, EventArgs e) { string filename = "C: \ Documents and Settings \ rmollo \ Escritorio \ prueba.txt"; FileStream stream = new FileStream(filename , FileMode.Create, FileAccess.Write ); BinaryWriter write = new BinaryWriter(stream); Int32 mVariable = textBox1.TextLength; Int32 mVar1; Byte[] mBuffer = System.IO stream.Close(); textBox1.Text = System.Convert.ToString(mVariable); } private void button2_Click(object sender, EventArgs e) { FileStream readStream = new FileStream("C: \ Documents and Settings \ rmollo \ Escritorio \ prueba.txt", FileMode.Open); BinaryReader readBinary = new BinaryReader(readStream
sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim stream As FileStream = New FileStream("C: \ Documents and Settings \ s_tcorp \ Escritorio \ prueba.txt", FileMode.Open, FileAccess.Write) Dim writer As BinaryWriter = New BinaryWriter(stream) Dim mVariable As Integer = Len(TextBox1.Text) writer sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim stream As FileStream = New FileStream("C: \ Documents and Settings \ s_tcorp \ Escritorio \ prueba.txt", FileMode.Open, FileAccess.Read) Dim reader As BinaryReader = New BinaryReader(stream) Dim mVariable As Integer mVariable
Add(new Criteria(kvp.Key, kvp.Value)); } try { using (Stream stream = File.Open("data.bin", FileMode.Create)) { BinaryFormatter bin = new BinaryFormatter(); bin.Serialize(stream, criteriaList); } } catch (IOException) { Debug.Print("error storing criteria string, string> rtnDict = new SortedDictionary<string, string> (); try { using (Stream stream = File.Open("data.bin", FileMode.Open)) { BinaryFormatter bin = new BinaryFormatter(); var criteriaList = (List<Criteria> )bin.Deserialize(stream); foreach (Criteria criteria can i fill the byref input dictionary? / / _criteriaDictionary = rtnDict; } } C# Discussions IOException (1) BinaryFormatter (1) FileStream (1) FileAccess (1) EventArgs (1) FileMode (1) Declare (1) Class (1) Both SortedDictionary and String are serializable, so I think the void Save<TKey, TValue> (this SortedDictionary<TKey, TValue> d, string fnm) { using(Stream stm = new FileStream(fnm, FileMode.Create, FileAccess.Write)) { BinaryFormatter fmt = new BinaryFormatter(); fmt.Serialize(stm, d); } } public static SortedDictionary<TKey
Reading data from FileStream and saving it to local disk .NET Framework hi i have a FileStream databse and filestream column in a table the column name is ItemImage i saved gif file at first fileName As String = "C: \ Users \ Ammar S. Mitoori \ Ahraz.docx" Dim stream As New IO.FileStream(fileName, IO.FileMode.Create) Dim writer As New IO.StreamWriter(stream) writer.Write(buffer) writer.Close() 'Cleanup con.Close Visual Studio .NET Discussions SQL Server 2008 (1) ItemImage.PathName (1) Word (1) FileAccess.Write (1) FileMode.Create (1) FileShare.Write (1) Hi Ammar, For FileStream data, you should use
SymmetricKeyAlgorithmTag.Aes128 (1) PgpUtilities.GetDecoderStream (1) PGPWrapper.Encrypt (1) PGPWrapper.Decrypt (1) PgpLiteralData.Binary (1) FileAccess.Write (1) Streams.PipeAll (1) Why do not you just use the same library you used BUFSIZ = 10240; public static void Encrypt(string infnm, string outfnm, string pp) { Stream instm = new FileStream(infnm, FileMode.Open, FileAccess.Read); Stream outstm = new FileStream(outfnm, FileMode.Create, FileAccess.Write); PgpEncryptedDataGenerator pedg = new PgpEncryptedDataGenerator(SymmetricKeyAlgorithmTag.Aes128, new SecureRandom()); pedg.AddMethod(pp.ToCharArray()); Stream