【问题标题】:System.UnauthorizedAccessException when launch application by link通过链接启动应用程序时的 System.UnauthorizedAccessException
【发布时间】:2015-11-19 10:14:10
【问题描述】:

我已经编写了一个应用程序(C#,VS2013),并添加了安装(flexera)。 安装过程提供复制文件到 ProgramFiles 目录并在桌面上创建快捷方式(链接)。 所以,当我点击快捷方式时 - 程序没有启动,并且在 Windows 事件日志中我看到了这些异常:

Сведения об исключении: System.UnauthorizedAccessException Стек: в System.IO.__Error.WinIOError(Int32, System.String) × System.IO.Directory.InternalCreateDirectory(System.String, System.String, System.Object, 布尔值) × System.IO.Directory.InternalCreateDirectoryHelper(System.String, 布尔值) × main_windows.Settings.Log(System.String) × main_windows.Settings..ctor() в main_windows.Program.Main()

如果我以管理员身份启动快捷方式 - 一切正常。

那时,如果我去安装程序的文件夹并点击 exe-file - 它会启动并且不需要以管理员身份启动它。

我尝试了不同的方法来解决这个问题,通过更改文件夹和文件的权限,包括关闭/打开继承,但未成功...

问题 - 我能做些什么来解决我的问题。

【问题讨论】:

  • 我认为这与应用程序的工作目录有关。能否请您右键单击桌面上的图标并查看“开始于”文本框中指定的文件夹?当您从安装文件夹启动程序时,工作目录将是该文件夹本身,因此您的应用程序将正常工作。
  • 工作目录是否正确(等于安装文件夹),我先检查一下。
  • 什么是设置类?
  • 因为它试图在启动过程中记录一些东西。您能否发布您的 main_windows.Program.Main() 方法的代码?
  • 是的,这是尝试。在主要我得到互斥锁检查应用程序已经运行。还有一些文件操作。代码在手下 - 稍后会发布它。我认为这并不重要——因为作为管理员并从“发布”运行没有问题。

标签: c# windows permissions


【解决方案1】:
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;
                        }
                    }


                }
            }
        }
    }
}

【讨论】:

    【解决方案2】:
    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();
    }
    

    【讨论】:

    • 您正在尝试在 Program Files\YourCompany\YourSoftware\logs 下创建一个目录,这会失败,是的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-15
    • 2012-04-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多