简体   繁体   中英

c# winforms GetCurrentDirectory returns C:\WINDOWS\System32\ design error

I'm making like a cashier app with a winforms project and i need to use an access database and i used System.IO.GetCurrentDirectory() . But it returns C:\\WINDOWS\\System32 on design time so it keeps popping up message boxes when i'm using the designer.

It also returns the correct directory when i run it.

Here's an example of my connection string:

OleDbConnection conn = new OleDbConnection();
string dataPath = System.IO.Directory.GetCurrentDirectory();
conn.ConnectionString = @"
Provider=Microsoft.ACE.OLEDB.12.0;
Data Source=" + dataPath + "/Data/Databases/database.accdb;Persist Security Info=False";

Any help will be appriciated.

Edit:

What happens when i try to open the designer: https://i.stack.imgur.com/HQjHa.png

Not sure why this is happening but u can try :

System.Environment.CurrentDirectory

//or

Application.StartupPath

//or

System.IO.Path.GetDirectoryName(Application.ExecutablePath);

//or

System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);

//or

System.AppDomain.CurrentDomain.BaseDirectory

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM