CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Oct 2011

    Newbie needs help with If statment

    Greetings all,

    First of all I want you all to know that I'm very new to C# so I may not be doing this correctly.
    I am using 2005 C# 2.0 .Net version

    I am currently collecting data from a power monitor and storing that data into a SQL database.
    What I would like to do is have C# create a folder with the folder name of Power Monitor and then create a subfolder of the current month as the folder name and within that subfolder, save the database file with the day as the file name.

    I have that part working (at least I think I do). What I am needing help with is trying to make this automated so that once the program executes it will update/create these folders/files on its own.

    My thinking is that I need to include an If statement within my program. I have researched and tried various ways but I can't seem to get it working properly. Here is the code I have so far:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    using System.Data.SqlClient;

    namespace PowerMonitor
    public partial class Form1 : Form

    System.Data.SqlClient.SqlConnection sqlConnection1 = new System.Data.SqlClient.SqlConnection
    ("data source=Chris\\SQLEXPRESS;initial catalog=Test;integrated security=SSPI;persist security info=False;workstation id=JOHN;packet size=4096");

    public Form1()

    public void Main()
    DateTime now = DateTime.Now;

    DateTime lastDayofCurrentMonth = new DateTime(DateTime.Today.Year,
    DateTime.Today.Month, DateTime.DaysInMonth(DateTime.Today.Year,

    lblDate2.Text = lastDayofCurrentMonth.ToString("MMM d yyyy");

    // Specify a "currently active folder"
    string activeDir = @"c:\PowerMonitor";

    // Creates a new subfolder under the current active folder for the current month
    string newPath = System.IO.Path.Combine(activeDir, now.ToString("MMM yyyy"));

    // Create the subfolder

    // Create a new file name.

    string newFileName = now.ToString("dddd d");
    lblDate.Text = newFileName.ToString();

    // Combine the new file name with the path
    newPath = System.IO.Path.Combine(newPath, newFileName);

    // Create the file and write to it.
    // DANGER: System.IO.File.Create will overwrite the file
    // if it already exists.

    if (!System.IO.File.Exists(newPath))
    using (System.IO.FileStream fs = System.IO.File.Create(newPath))
    for (byte i = 0; i < 100; i++)

    // Read data back from the file to prove
    // that the previous code worked.

    byte[] readBuffer = System.IO.File.ReadAllBytes(newPath);
    foreach (byte b in readBuffer)
    catch (System.IO.IOException e)

    private void btnSubmit_Click(object sender, EventArgs e)
    string selectRunCMD = "SELECT SUM(Raw_Current_Input) AS Value from Test";
    SqlDataAdapter selectRunAdapter = new SqlDataAdapter(selectRunCMD, sqlConnection1);
    DataTable selectRunTbl = new DataTable();
    DataRow selectDataRow = selectRunTbl.Rows[0];
    decimal data = decimal.Parse(selectDataRow["Value"].ToString());
    lblData.Text = data.ToString();


    Thanks for any advice/help in advance.


  2. #2
    Join Date
    Jul 2001
    Sunny South Africa

    Re: Newbie needs help with If statment

    Use code tags when posting code. It is explained here :


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.