Click here to Skip to main content
15,919,613 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I managed to create a database using linq to sql with the following code:

C#
private static DataClasses1DataContext _dataDC = new DataClasses1DataContext(@"C:\\database1.mdf");

public AddClient()
{
    InitializeComponent();
    DataContext = this;
    _dataDC.CreateDatabase();
}


and this created a new local database on C: .

How can i change the @"C:\\database1.mdf" so that the file is created in the current directory from where i run the program ?
Posted

1 solution

Try:
C#
string path = Application.ExecutablePath;
string betterPath = Application.CommonAppDataPath;
The first gets the executable application EXE file - so you can use the folder by:
string justPath = Path.GetDirectoryName(path);
But it is a much, much better idea to use the DataPath - you may well havce problems writing to the EXE path in release versions as it will be below the "Program Files" folder and require Admin access.
 
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