简体   繁体   English

通过链接启动应用程序时出现System.UnauthorizedAccessException

[英]System.UnauthorizedAccessException when launch application by link

i've been wrote an application (C#, VS2013), and add installation to it (flexera). 我已经编写了一个应用程序(C#,VS2013),并向其中添加了安装(flexera)。 Installation process provide copy files to ProgramFiles direcotiry and create a shortcut(link) on desktop. 安装过程提供复制文件到ProgramFiles目录,并在桌面上创建快捷方式(链接)。 So, when i click on shortcut - program doesn't start, and in windows event log i see those exceptions: 因此,当我单击快捷方式-程序无法启动时,在Windows事件日志中,我看到了以下异常:

Сведения об исключении: System.UnauthorizedAccessException Стек: в System.IO.__Error.WinIOError(Int32, System.String) в System.IO.Directory.InternalCreateDirectory(System.String, System.String, System.Object, Boolean) в System.IO.Directory.InternalCreateDirectoryHelper(System.String, Boolean) в main_windows.Settings.Log(System.String) в main_windows.Settings..ctor() в main_windows.Program.Main() 说明:System.UnauthorizedAccessException说明:•System.IO .__ Error.WinIOError(Int32,System.String)•System.IO.Directory.InternalCreateDirectory(System.String,System.String,System.Object,Boolean)•系统。 IO.Directory.InternalCreateDirectoryHelper(System.String,Boolean)•main_windows.Settings.Log(System.String)•main_windows.Settings..ctor()•main_windows.Program.Main()

if i start shortcut as administrator - everything fine. 如果我以管理员身份启动快捷方式-一切正常。

at that time, if i go to folder with installed program and click on exe-file - it starts and there is not need to start it as admin. 当时,如果我进入已安装程序的文件夹并单击exe文件-它会启动,无需以管理员身份启动它。

I try different ways to solve this, by changing permissions to folder and file, including switch off/on inheritance, but unsusccessfull... 我尝试通过更改文件夹和文件的权限来解决此问题的方法,包括关闭/打开继承,但不成功...

Question - What can i do, to solve my problem. 问题-我该怎么办才能解决我的问题。

namespace main_windows
{

    static class Program
    {
        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool SetForegroundWindow(IntPtr hWnd);
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            bool createdNew = true;
            using (Mutex mutex = new Mutex(true, "Settings", out createdNew))
            {
                if (createdNew)
                {
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    Application.Run(new Settings());
                }
                else
                {
                    Process current = Process.GetCurrentProcess();
                    foreach (Process process in Process.GetProcessesByName(current.ProcessName))
                    {
                        if (process.Id != current.Id)
                        {
                            SetForegroundWindow(process.MainWindowHandle);
                            break;
                        }
                    }


                }
            }
        }
    }
}
private void Log(string s)
{

    string file = Application.ExecutablePath.Substring(0, Application.ExecutablePath.LastIndexOf('\\'));
    file += "\\logs\\";
    if (!Directory.Exists(file))
        Directory.CreateDirectory(file);
    file += DateTime.Now.Date.Year.ToString();
    file += "-" + DateTime.Now.Date.Month.ToString();
    file += "-" + DateTime.Now.Date.Day.ToString();
    file += (".log");
    FileStream fs = new FileStream(file, FileMode.Append);
    StreamWriter sw = new StreamWriter(fs);

    sw.WriteLine(DateTime.Now.Hour.ToString("00")+ ":" +
                 DateTime.Now.Minute.ToString("00") + ":" +
                 DateTime.Now.Second.ToString("00") + " " + s);
    sw.Close();
    fs.Close();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM