Click here to Skip to main content
15,911,646 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Here is the most basic of programs ...

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace KAELCtest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            string path;
            path = Application.StartupPath;
            MessageBox.Show(path);
        }
    }
}


If I run this in VS2010 debug mode I get the following ...

http://www.4shared.com/photo/_4PP26aA/ScreenHunter_03_Mar_29_1607.html[^]

If I now Publish the program to J:\Users\Gary\Documents\Visual Studio 2010\Published Projects\KAELCtest\ and I run this Setup program, as I would on another PC, it now points to an obscure Folder ...

http://www.4shared.com/photo/XVpUuaIX/ScreenHunter_03_Mar_29_1614.html[^]

Why is this ? What is the point of finding the path of your executing program in a C# program if Publishing your program changes this path ?!?

I want to copy my Setup & associated files onto a USB and run this Setup on a new PC. The path is used to find the associated files, but if Windows is changing that path, how am I to do this ?!?
Posted

This is by design MSDN[^]:
Application.StartupPath Property Value

Type: System.String
The path for the executable file that started the application.
This path will be different depending on whether the Windows Forms application is deployed using ClickOnce. ClickOnce applications are stored in a per-user application cache in the C:\Documents and Settings\username directory. For more information, see Accessing Local and Remote Data in ClickOnce Applications.

So, it is returning the same path - it's just that your program is not where you thought it would be...


"Thanks for replying OriginalGriff ... I've read something about Clickonce doing this, but can't get it explained ... I've had a problem trying to resolve this for a week now and am just getting nowhere !!! To be honest, I don't give a damn where the Setup program is or where it runs from, but I use the Path to get at the other data & folders I am deploying on the new machine. These could be on a USB or CD with any drive letter assigned, or indeed on any drive under any Folder, if my user copies them before running them. So how do I know my Path to access these files & folders, which are NOT moved to the username directory ?!?"


The best thing to do is to use a folder which can move, but which windows keeps a track of, and which full access is granted to all users - which is not the case for the folder the application is installed in!

There is a folder that fits the bill: The Common Application Data Folder - it is user independent (so all users of the computer use the same data and can read and write to it) If you create a folder beneath that and store your data in there, it will be easy to find.
The base directory is accessed thus:

C#
string basePath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
If you append your application name (or better a fixed GUID value) and move everything to there it should be fine.
 
Share this answer
 
v2
Comments
Gary Heath 30-Mar-12 8:27am    
Thanks for replying OriginalGriff ... I've read something about Clickonce doing this, but can't get it explained ... I've had a problem trying to resolve this for a week now and am just getting nowhere !!! To be honest, I don't give a damn where the Setup program is or where it runs from, but I use the Path to get at the other data & folders I am deploying on the new machine. These could be on a USB or CD with any drive letter assigned, or indeed on any drive under any Folder, if my user copies them before running them. So how do I know my Path to access these files & folders, which are NOT moved to the username directory ?!?
OriginalGriff 30-Mar-12 9:49am    
Answer updated
I gave up with this, C# is just too damned complicated & difficult. I am starting from scratch using VB.net instead, but will refer to this when the time is right, it makes sense, thanks ...
 
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