识别程序是否从Desktop运行

时间:2014-07-06 09:09:17

标签: c#

有没有办法识别应用程序是否已从快捷方式而不是可执行文件运行?我需要让我的用户将exe文件复制到他们的桌面,而不是由于个性化问题而创建它的快捷方式。有什么想法吗?

编辑:创建安装程序不是一种选择。

2 个答案:

答案 0 :(得分:0)

我不知道这是否有帮助,但是如果你想让你的exe文件在桌面上,这可能会有效:

string path = Directory.GetCurrentDirectory();
string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
if (!path.Equals(desktopPath))
{
    Console.WriteLine("file is not at desktop");
}

答案 1 :(得分:0)

如果您在Windows共享文件夹中有应用程序,则可以configure it来阻止执行应用程序。

或者,您可以向用户提供指向.bat文件的链接,而不是.exe,并且会执行此类操作(使用robocopy):

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

在C#方面,您可以check application path并执行以下操作:

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
    }
}