Share via


How to read the path of the folder inside the Project directory using c#

Question

Monday, July 30, 2012 11:19 AM

I am having a folder inside the Project directory . Inside the project directory there are other folders .

that is ,

the project directory contains a folder named Prodata . 

inside that there are layer1 ,layer 2 etc .

inside that there are lots of bmp files.

i want to take the path of the files inside the layer 1.

i am using the code  

string path = Server.MapPath(@"ProdData");

but it will returns the full path of the file . 

what i need is the path starting from ProdData.

my entire code is like this

public void InsertImage()

{

string path = Server.MapPath(@"ProdData");
     

        string[] filePaths = Directory.GetFiles(path, "*.bmp",SearchOption.AllDirectories);
        string connectionString = "Server=localhost;Port=3306;Database=newproj;Uid=root;Pwd=welcome";

        MySqlConnection conn = new MySqlConnection(connectionString);

        foreach (string st in filePaths)
        {
            string query = "insert into imagedetails value('" + st+ "')";
            MySqlCommand cmd = new MySqlCommand(query, conn);
            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();
        }

}

MSDN Forum

All replies (4)

Thursday, August 2, 2012 5:05 AM ✅Answered

Hi Amal E S,

  Turning relative path into absolute path nead a reference absolute path (and the relative path location), for your example, maybe it is the absolute path of the  main executable project.

- For Windows applications, you can use Application.ExecutablePath property.

- For the Web project, you can use Server.MapPath to directly convert the relative path of application  into absolute physical path. After knowing the absolute path, use Path.Combine to merge these two path for getting a wanted relative path. Or an easier way, use System.Uri.TryCreate method.

  Sincerely,

  Jason Wang

Jason Wang [MSFT]
MSDN Community Support | Feedback to us


Thursday, August 2, 2012 6:01 AM ✅Answered

See if this sample helps:

string s1 = @"D:\MyProject\ProData\";
string s2 = @"D:\MyProject\ProData\Layer 4\image1.bmp";
 
Uri u1 = new Uri(s1);
Uri u2 = new Uri(s2);
                           
string relative = Uri.UnescapeDataString(u1.MakeRelativeUri(u2).OriginalString);

Monday, July 30, 2012 11:25 AM

try to change 

**string path = Server.MapPath(@"ProdData");  **TO string path = Server.MapPath(@"ProdData/layer1"); it will return what you want that only


Monday, July 30, 2012 11:30 AM

The ProData is the only folder available when the software starts. And the path must be stored inside the database starting from @Prodata.

There are soo many folders inside the Prodata , so cant predict the next folder names . 

and what i need is to take the path starting from ProData / Layer4/image1.bmp

not like D:\MyProject\ProData\Layer 4\image1.bmp

MSDN Forum