Sanjeev
Sanjeev

Reputation: 417

Startup location is different from the application's location .

I need to start my c# application at windows start up. I have tried to add registry key with the application's exe path by using the following two options.

System.Environment.CurrentDirectory
Directory.GetCurrentDirectory()

But when the windows starts, the folder path is showing to system32 directory instead of the application's directory. So, the code which depends on the current directory is not working properly. How to solve this to achieve to the programs folder ?? I have searched the related posts but no solution is obtained.

Thank you,


This code works for me:

Directory.GetParent(System.Windows.Forms.Application.ExecutablePath)

this gives the directory where the application is placed.

Thanks a lot.

Upvotes: 1

Views: 193

Answers (5)

Bali C
Bali C

Reputation: 31251

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

Hope this helps!

Upvotes: 0

cincura.net
cincura.net

Reputation: 4160

You can try

System.Reflection.Assembly.GetExecutingAssembly().Location

Upvotes: 0

kyrylomyr
kyrylomyr

Reputation: 12652

You can retrieve path from the location of current assembly:

var assembly = System.Reflection.Assembly.GetEntryAssembly();
var curDir = System.IO.Path.GetDirectoryName(assembly.Location);

Upvotes: 1

keyboardP
keyboardP

Reputation: 69392

var myDir = AppDomain.CurrentDomain.BaseDirectory;

AppDomain.CurrentDomain.BaseDirectory on MSDN

Upvotes: 1

parapura rajkumar
parapura rajkumar

Reputation: 24433

I think you need to use the exe path here not the current directory. Look into System.Reflection.Assembly.GetExecutingAssembly and then Assembly.Location

Upvotes: 1

Related Questions