Noob needs a little help with C#

BikerEcho

Fully Optimized
Messages
4,029
Location
Denmark
Hey gang.
I am messing around with programming again.
I did a little basic programming under my education, but i haven't even looked at coding for 3 years now.

I want to split the time into hours and minutes and place them in 2 variables.
int hour;
int minute;

How do i do this?
 
I am using the code: DateTime.Now.ToString()
It gives me what i need... + the date, which i am not interested in.
I know i can split the string and put the data i need into an array.
how do i do that?
 
I figured it out guy.
This is the code. I am sure there is a better way to do it, but it works.

string Date = DateTime.Now.ToString();
string[] DateTimeSplitter = Date.Split(' ');
string[] TimeSplitter = DateTimeSplitter[1].Split(':');
double Hour = Convert.ToDouble(TimeSplitter[0]);
double Minute = Convert.ToDouble(TimeSplitter[1]);
 
Could you not just do something like:

DateTime now = DateTime.Now;
int hour = now.Hour;
int minute = now.Minute;

?

Been a long time since I've touched C# though!
 
... that actually worked. :lol:
Well... i learned some array by doing this work around.

Thanks for the tip. :thumb:
 
I have another question.

I want to add the text "RANDOM" to a file i created called DBtextfile.txt.
It's located at C:\Users\Administrator\Desktop\Data\DBtextfile.txt

i will also want to reed the file again and place the text "RANDOM" in a string called:
string capture;

How can i do this?
I have checked some videos on youtube, but non of them uses an existing file or a file not located in the same folder as the .exe file.
They use the "StreamWriter" and "StreamReader" function. Any ideas?
 
Last edited:
I have another question.

I want to add the text "RANDOM" to a file i created called DBtextfile.txt.
It's located at C:\Users\Administrator\Desktop\Data\DBtextfile.txt

i will also want to reed the file again and place the text "RANDOM" in a string called:
string capture;

How can i do this?
I have checked some videos on youtube, but non of them uses an existing file or a file not located in the same folder as the .exe file.
They use the "StreamWriter" and "StreamReader" function. Any ideas?

You can use those examples fine, such as the one here (How to: Read Text from a File) - just replace the file name with the full path to the file. So:

Code:
using (StreamReader sr = new StreamReader("TestFile.txt"))

would become:

Code:
using (StreamReader sr = new StreamReader(@"C:\Users\Administrator\Desktop\Data\DBtextfile.txt"))
 
If I may...

Code:
var targetFile = @"C:\Users\Administrator\Desktop\Data\DBtextfile.txt";
var newFileText = new StringBuilder();

//Read original File
using(var reader = new StreamReader(targetFile)) {
    newFileText.AppendFormat(reader.ReadToEnd());
    reader.Close();
}

//Add changes
var lineBreak = @"\n";
newFileText.AppendFormat("{0}RANDOM{0}", lineBreak);

//Create File Backup if needed
var targetFileBackup = targetFile + "_" + DateTime.Now.ToFileTime().ToString();
if(File.Exists(targetFile))
  File.Move(targetFile, targetFileBackup);

//Write changes
using(var writer = new StreamWriter(targetFile)) {
   writer.Write(newFileText);
   writer.Close();
}

Please note, all that will do is open a file, add a line break, the word "RANDOM", another line break and rewrite the file to the disk.

What are you trying to do with this file that you need to read from...change...write to... ?

EDIT: You'll notice that I use "var" instead of variable types in the initial declaration statement. Here's the doc from M$ on it. Makes life a little easier sometimes, but you can only use if it's obvious what type of variable it will become. E.g. you can't do "var integerVariable = null;"... because anything can be null. A null integer has to be explicitly typed "int integerVariable = null;"
 
Last edited:
Thanks guys.
I tried out you huge code and it worked great. Thanks
It did however write something like "\nRANDOM\n". I suppose the \n should created a new line. I doesn't.
i have removed some of the code and added something to read it again.

The code now looks like this:

Code:
var targetFile = @"C:\Users\Administrator\Desktop\Data\DBtextfile.txt";
var newFileText = new StringBuilder();

            //Write changes
            using (var writer = new StreamWriter(targetFile))
            {
                writer.Write("test");
                writer.Close();
            }

            //Read from file
            using (StreamReader sr = new StreamReader(targetFile))
            {
                String line = sr.ReadToEnd();
                textBoxRead.Text=line;
            }


Every time i run the code, the previous text is replaced with text will be replaced. (it did the same thing before i removed half of the code)
how can i get around this?
Also a code to clear all the text in the file would be nice to have.
 
Last edited:
StreamWriter has a constructor that takes both a string and a boolean, the latter being the flag for if the file is appended to or overwritten: StreamWriter Constructor (String, Boolean) (System.IO)

So constructing one by doing "new StreamWriter(targetFile, true)" will append to the file.

To clear the file, just create a StreamWriter and write "string.empty" to the file.

Also worth pointing out that a quicker way of writing short bits of text to files without manually constructing the writer is to use File.WriteAllText() (File.WriteAllText Method (String, String) (System.IO)).
 
Back
Top Bottom