Go Back   Computer Forums > General Computing > Programming
Click Here to Login
Join Computer forums Today


Reply
 
Thread Tools Search this Thread Display Modes
 
Old 05-05-2014, 08:53 AM   #1
Fully Optimized
 
BikerEcho's Avatar
 
Join Date: Feb 2011
Location: Denmark
Posts: 4,013
Default Noob needs a little help with C#

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?
__________________

__________________
MOBO: Asus Maximus 6 Hero - GPU: Gigabyte GTX 980ti xtreme - CPU: I7-4770K - PSU: Corsair AX760
RAM: Corsair Vantage 2x4GB - SSD: Samsung 840 EVO 512 + LiteOn LCS-256 - Case: NZXT H440
BikerEcho is online now   Reply With Quote
Old 05-06-2014, 02:33 AM   #2
Fully Optimized
 
BikerEcho's Avatar
 
Join Date: Feb 2011
Location: Denmark
Posts: 4,013
Default Re: Noob needs a little help with C#

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?
__________________

__________________
MOBO: Asus Maximus 6 Hero - GPU: Gigabyte GTX 980ti xtreme - CPU: I7-4770K - PSU: Corsair AX760
RAM: Corsair Vantage 2x4GB - SSD: Samsung 840 EVO 512 + LiteOn LCS-256 - Case: NZXT H440
BikerEcho is online now   Reply With Quote
Old 05-06-2014, 03:41 AM   #3
Fully Optimized
 
BikerEcho's Avatar
 
Join Date: Feb 2011
Location: Denmark
Posts: 4,013
Default Re: Noob needs a little help with C#

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]);
__________________
MOBO: Asus Maximus 6 Hero - GPU: Gigabyte GTX 980ti xtreme - CPU: I7-4770K - PSU: Corsair AX760
RAM: Corsair Vantage 2x4GB - SSD: Samsung 840 EVO 512 + LiteOn LCS-256 - Case: NZXT H440
BikerEcho is online now   Reply With Quote
Old 05-06-2014, 05:31 AM   #4
Site Team
 
berry120's Avatar
 
Join Date: Jul 2009
Location: England, UK
Posts: 3,425
Default Re: Noob needs a little help with C#

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!
__________________
Save the whales, feed the hungry, free the mallocs.
berry120 is offline   Reply With Quote
Old 05-06-2014, 07:50 AM   #5
Fully Optimized
 
BikerEcho's Avatar
 
Join Date: Feb 2011
Location: Denmark
Posts: 4,013
Default Re: Noob needs a little help with C#

... that actually worked.
Well... i learned some array by doing this work around.

Thanks for the tip.
__________________
MOBO: Asus Maximus 6 Hero - GPU: Gigabyte GTX 980ti xtreme - CPU: I7-4770K - PSU: Corsair AX760
RAM: Corsair Vantage 2x4GB - SSD: Samsung 840 EVO 512 + LiteOn LCS-256 - Case: NZXT H440
BikerEcho is online now   Reply With Quote
Old 05-06-2014, 08:46 AM   #6
Fully Optimized
 
BikerEcho's Avatar
 
Join Date: Feb 2011
Location: Denmark
Posts: 4,013
Default Re: Noob needs a little help with C#

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?
__________________
MOBO: Asus Maximus 6 Hero - GPU: Gigabyte GTX 980ti xtreme - CPU: I7-4770K - PSU: Corsair AX760
RAM: Corsair Vantage 2x4GB - SSD: Samsung 840 EVO 512 + LiteOn LCS-256 - Case: NZXT H440
BikerEcho is online now   Reply With Quote
Old 05-06-2014, 11:17 AM   #7
Site Team
 
berry120's Avatar
 
Join Date: Jul 2009
Location: England, UK
Posts: 3,425
Default Re: Noob needs a little help with C#

Quote:
Originally Posted by BikerEcho View Post
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"))
__________________
Save the whales, feed the hungry, free the mallocs.
berry120 is offline   Reply With Quote
Old 05-06-2014, 11:39 AM   #8
..m.0,0.m..
Site Team
 
iPwn's Avatar
 
Join Date: May 2010
Location: USA
Posts: 3,870
Default Re: Noob needs a little help with C#

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;"
__________________
Me: You'd think as the dominant species we wouldn't be so effing stupid.
J: We're just intelligent enough to be completely effing stupid.
iPwn is offline   Reply With Quote
Old 05-07-2014, 02:51 AM   #9
Fully Optimized
 
BikerEcho's Avatar
 
Join Date: Feb 2011
Location: Denmark
Posts: 4,013
Default Re: Noob needs a little help with C#

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.
__________________
MOBO: Asus Maximus 6 Hero - GPU: Gigabyte GTX 980ti xtreme - CPU: I7-4770K - PSU: Corsair AX760
RAM: Corsair Vantage 2x4GB - SSD: Samsung 840 EVO 512 + LiteOn LCS-256 - Case: NZXT H440
BikerEcho is online now   Reply With Quote
Old 05-07-2014, 05:00 AM   #10
Site Team
 
berry120's Avatar
 
Join Date: Jul 2009
Location: England, UK
Posts: 3,425
Default Re: Noob needs a little help with C#

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)).
__________________

__________________
Save the whales, feed the hungry, free the mallocs.
berry120 is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



All times are GMT -5. The time now is 06:59 AM.


Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0