Click here to Skip to main content
16,005,120 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How do i return string with ID i want check folder is Exists or not but i use global static string must use internal variable in void what you suggested
C#
namespace vc
{
    public partial class Form1 : Form
    {
      public static string path_program;
        public static string path_program_log;
        public static string path_log;

   private void Form1_Load(object sender, EventArgs e)
        {
            creat_folder_log("//log"); // its promlem
            creat_folder("//image");
        }


      void creat_folder(string name_folder)
        {
            string n = System.IO.Path.GetDirectoryName(Application.ExecutablePath.ToString()).ToString() + name_folder;
            if (Directory.Exists(n) == false)
            {
                System.IO.Directory.CreateDirectory(n);
            }
            path_program = n;
        }

   void creat_folder_log(string name_folder)
        {
            string n = System.IO.Path.GetDirectoryName(Application.ExecutablePath.ToString()).ToString() + name_folder;
            if (Directory.Exists(n) == false)
            {
                System.IO.Directory.CreateDirectory(n);
            }
            path_program_log = n;
        }
    }
}
Posted
Updated 16-Oct-14 0:13am
v2

An alternative solution could be this.

C#
using System.IO;

namespace vc
{
    public partial class Form1 : Form
    {
        // The use of properties instead of fields encapsulates the data
        // the private set means that the property can't be changed outside the class
        public string path_program { get; private set; }
        public string path_program_log { get; private set; }
        public string path_log { get; private set; }
        
        private void Form1_Load(object sender, EventArgs e)
        {
            // By using Path.Combine inside create_folder the need
            // of slashes in the parameter is eliminated
            path_program = create_folder("log");
            path_program_log = create_folder("image");
        }
        
        // Only one method is necessary as you have the folder name as a parameter
        private string create_folder(string name_folder)
        {
            string n = Path.Combine(
                Path.GetDirectoryName(Application.ExecutablePath), 
                name_folder));
            if (!Directory.Exists(n))    // Using the ! (not) operator
            {
                Directory.CreateDirectory(n);
            }
            return n;
        }
        
    }
}
 
Share this answer
 
v3
Comments
BillWoodruff 16-Oct-14 7:48am    
+5 Excellent !
George Jonsson 16-Oct-14 8:40am    
Thanks Bill.
To be honest I am not sure this is the wanted functionality, though.
majid torfi 16-Oct-14 8:43am    
thanks
just i change path_program_log = create_folder("//image"); to create_folder("image");
but use { get; private set; } and Path.Combine is good idea
George Jonsson 16-Oct-14 8:59am    
You are welcome.
I saw that error and updated my answer with the correction.
The copy&paste devil again. :P
Try this;

C#
namespace vc
{
    public partial class Form1 : Form
    {
        public string path_program;
        public string path_program_log;
        public string path_log;

   private void Form1_Load(object sender, EventArgs e)
        {
            path_program = creat_folder_log("//log"); // its promlem
            path_program_log = creat_folder("//image");
        }


      string creat_folder(string name_folder)
        {
            string n = System.IO.Path.GetDirectoryName(Application.ExecutablePath.ToString()).ToString() + name_folder;
            if (Directory.Exists(n) == false)
            {
                System.IO.Directory.CreateDirectory(n);
            }
            return n;
        }

   string creat_folder_log(string name_folder)
        {
            string n = System.IO.Path.GetDirectoryName(Application.ExecutablePath.ToString()).ToString() + name_folder;
            if (Directory.Exists(n) == false)
            {
                System.IO.Directory.CreateDirectory(n);
            }
            return n;
        }
    }
}


Hope this helps,
Fredrik
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900