0

Is there any way to recognize if the application has been run from a shortcut instead of executable file? I need to make my users to copy exe file to their desktops rather than create shortcuts to it due to personalization issues. Any ideas?

Edit: creating the installer is not an option.

Luka
  • 23
  • 4
  • 3
    It doesn't seem to be the right approach. – VahidNaderi Jul 06 '14 at 09:16
  • 5
    I think that you're trying to solve the wrong problem. A program should never be directly on the desktop and personalization issues shouldn't be dependent on the location of the program file. Can you elaborate on what you are trying to achieve and what the personalization issues are? – Anders Abel Jul 06 '14 at 09:17
  • 3
    Maybe you should ask your question about your personalization issue instead. – nvoigt Jul 06 '14 at 09:17
  • 1
    I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Jul 06 '14 at 09:21
  • small simple application that is using .ini file to save some settings. If more than one user is using same exe file from a network drive, settings are not accurate anymore. I'd like to put some notification for the user. – Luka Jul 06 '14 at 09:21
  • And, why is an installer not an option? It's the right way to solve this problem. BTW, you're not the first person with this problem. The solution was to use an installer. – John Saunders Jul 06 '14 at 09:23
  • Isn't the correct question then »Am I being run from a network share?« instead of »Am I being run via a shortcut?«. Or you could store the settings in the user's profile instead of next to the application (where settings usually don't reside in the days of `C:\Program Files` being read-only for normal users). – Joey Jul 06 '14 at 09:23
  • @Јοеу thank you for your comment. You're probabbly right. I should check the network drive. Moving settings file to the user's profile. That should solve the problem for good. – Luka Jul 06 '14 at 09:30

2 Answers2

0

I don't know if this helps, but if you want your exe file to be on the desktop, this could work:

string path = Directory.GetCurrentDirectory();
string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
if (!path.Equals(desktopPath))
{
    Console.WriteLine("file is not at desktop");
}
Mehraban
  • 3,164
  • 4
  • 37
  • 60
  • @Luka: I hope I never have to use your program. Introducing such weird and artificial constraints can cause a real headache down the road. I strongly urge you to reconsider your design. – MicroVirus Jul 06 '14 at 10:43
  • @MicroVirus: thanks for your suggestion and your kind words. I can assure you this single project is very specific. Work on this one started back in 2011 when I had literally no knowledge about programming (not much has changed since then). Redesigning the whole thing at this point would take way to much time and effort. And lastly, I'm sure you won't have to use any of my programs. Regards, Luka. – Luka Jul 06 '14 at 11:10
  • @Luka I suggest to change the question title, since the answer won't address current title. – Mehraban Jul 06 '14 at 11:17
  • I hope the domain admin will activate the policy which enforces that executables are run from "trusted" locations (e.g. C:\Program Files, C:\Windows) only... – Bernhard Hiller Jul 07 '14 at 08:30
0

If you do have an app in the windows shared folder you can configure it to prevent execution of the applications.

Or you can provide user just with link to .bat file instead of .exe and it would do something like this (using robocopy):

robocopy \\remote\server\exe %AppData%\your\folder app.exe /XO
start %AppData%\your\folder\app.exe

And on the C# side you can just check application path and do something like this:

public class Program 
{
    public int Main()
    {
        string original_path = System.IO.Path.GetFullPath(@"\\remote\app.exe");
        string current_path = System.IO.Path.GetFullPath(
            System.Reflection.Assembly.GetExecutingAssembly().Location);

        if(original_path == current_path){
            System.IO.File.Copy(original_path, @"C:\foo\bar\app.exe", true);
            System.Diagnostics.Process.Start(@"C:\foo\bar\app.exe");
            return 0;
        }

        // Run program normally here
    }
}
Community
  • 1
  • 1
Vyktor
  • 20,559
  • 6
  • 64
  • 96