check that string key entered correctly in the entry point of c# program?



My string key is "-c".


correct :



app.exe -c config.xml


wrong :


app.exe config.xml // already done


app.exe -d config.xml // ?


/* wrong, please input key '-c' for xml file. Use: app.exe -c config.xml */


I tried to do it in ways but getting different compile errors.


My attempts:



String ckey = args[0];

String akey = "-c";


1)



bool test1 = ckey.StartsWith("-c");


2)



bool TextCompare = false ;

if (int StrCmp(ckey, akey, bool TextCompare ) != 0 )

{
fin = new FileStream(args[1], FileMode.Open);
}


3)



if (ckey == akey) {...}

else if (ckey == "-i") { ... }

else Console.WriteLine("Please input correct key");


4)



if (args[0] == "-c") { ... }


5)



bool Check(akey, ckey)
if (Check) {...}

int result = String.Compare(akey, ckey);
bool res = Convert.ToBoolean(result);


App.cs:



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int i;
FileStream fin;
if (args.Length != 2)
{
Console.WriteLine("Use: app.exe -c config.xml");
return;
}
try
{
fin = new FileStream(args[1], FileMode.Open);
}
catch (IOException exc)
{
Console.WriteLine("Unable to open file");
Console.WriteLine(exc.Message);
return;
}
try
{
do
{
i = fin.ReadByte();
if (i != -1) Console.Write((char)i);
} while (i != -1);
}
catch (IOException exc)
{
Console.WriteLine("Failed to read file");
Console.WriteLine(exc.Message);
}
finally
{
Console.ReadLine();
}
}
}
}

No comments:

Post a Comment