Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Windows VS2010
Here is the most basic of programs ...
 
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 30-Mar-12 3:03am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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:
 
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.
  Permalink  
v2
Comments
Gary Heath at 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 at 30-Mar-12 9:49am
   
Answer updated
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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 ...
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 386
1 Marcin Kozub 225
2 Sergey Alexandrovich Kryukov 215
3 Maciej Los 169
4 /\jmot 169
0 OriginalGriff 8,289
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,624
3 Maciej Los 4,989
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 14 Jul 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100