0

I have created a WindowsApp.exe under Application\Setup folder. Now I want to create a Database under Application\Database folder from WindowsApp.exe

What should be the path given to the filename here?

Josh E
  • 7,390
  • 2
  • 32
  • 44
CPK_2011
  • 872
  • 3
  • 21
  • 57
  • you mean in a folder named database where application is installed? – Karthik Jul 06 '13 at 13:22
  • Application\Setup\WindowsApp.exe is the exe location. Application\Database\db1.mdb is database location. Now I am running WindowsApp.exe and want to create db1.mdb as mentioned – CPK_2011 Jul 06 '13 at 13:24
  • try the relative path "Database\db1.mdb" – Ron.B.I Jul 06 '13 at 13:25
  • Thanks for the immediate replies. But WindowsApp.exe is running under Setup folder. We need to go back one step and then access Database folder – CPK_2011 Jul 06 '13 at 13:26
  • in that case simply use "..\Database\db1.mdb" – Ron.B.I Jul 06 '13 at 13:27
  • http://stackoverflow.com/questions/5908343/getting-root-folder-of-application – Karthik Jul 06 '13 at 13:28
  • Thanks a lot. But point here. If WindowsApp.exe is under Application\Main\Setup\WindowsApp.exe and Database is just under Application\Database. Now I want to come back 2 steps back and use Database\db1.mdb. So if I put one more dot "." like this to "...\Database\db1.mdb", Will this work? – CPK_2011 Jul 06 '13 at 13:33
  • if you want it under application then simply reach that folder by getting the current folder which is a child and cut the rest of the path – Mahmoud Darwish Jul 06 '13 at 13:34
  • look at the updated answer – Mahmoud Darwish Jul 06 '13 at 13:39

1 Answers1

3

try

    Path.Combine(Environment.CurrentDirectory, "Database\\db1.mdb")

Edit

since you want the parent folder you can go up one folder by doing

    Path.Combine(Environment.CurrentDirectory.Substring(Environment.CurrentDirectory.LastIndexOf("\\")), "Database\\db1.mdb")

Edit 2

if you want the Application folder even if its N times above the current folder then you can reach it by doing this

    var index = Environment.CurrentDirectory.IndexOf(Environment.CurrentDirectory.IndexOf("ApplicationRootFolderName"),"\\")
    var AppRootPath = Environment.CurrentDirectory.Substring(0,index);

Edit 3

As mentioned by Michael its better to get the parent folder using this way

    Directory.GetParent(Environment.CurrentDirectory).FullName
Community
  • 1
  • 1
Mahmoud Darwish
  • 1,168
  • 1
  • 15
  • 28