Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

C# how do i write a folder from a form

  • 31-03-2012 12:51PM
    #1
    Registered Users, Registered Users 2 Posts: 620 ✭✭✭


    Hi Guys,

    I have a forms application written in C# via a textbox i am writing to a flat file at a directory i have hardcoded as in i have to install it separate manually to disc for for the file writing and reading to work this folder for instance is c:\AB\RESOURCES. It contains things like pdf files which i can open via a menu from the forms and then will contain flat files that i write via the textbox on one of my forms, and then later another form can read this same file.


    is there anyway that i can write this directory automatically in c#

    any help appreciated

    tks

    neonitrix


Comments

  • Registered Users, Registered Users 2 Posts: 8,658 ✭✭✭Trampas


    Sorry what do you mean write this directory?

    Create the directory?


  • Registered Users, Registered Users 2 Posts: 620 ✭✭✭neonitrix


    yes sorry - I mean create a directory the first time the program runs so i can write files to it. at the moment i have hardcoded a location that is being read if the file dosnt exist a catch is outputting an error.

    tks

    neonitrix


  • Closed Accounts Posts: 17,208 ✭✭✭✭aidan_walsh




  • Registered Users, Registered Users 2 Posts: 1,017 ✭✭✭The_Thing


    Hi, I think this might do what you want - MainForm is the name of my form.
    private void MainForm_Shown(object sender, EventArgs e)
    {

    CenterToScreen();

    CheckForBaseFolder();

    }
    private void CheckForBaseFolder()
    {

    if (Directory.Exists(Configuration.BaseFolder))
    {



    }
    else
    {

    Directory.CreateDirectory(Configuration.BaseFolder);

    }

    }

    Configuration is a static class which I use to store all my 'magic numbers', strings, etc, as it helps to keep other classes free of clutter.
    class Configuration
    {

    //The name of our auto-generated 'Base' folder - change this string to suit your needs.
    private static string basefolder = @UserGeneratedContent;

    public static string BaseFolder
    {

    get { return basefolder; }

    }

    }


Advertisement