C# 通过链接启动应用程序时System.UnauthorizedAccess异常

C# 通过链接启动应用程序时System.UnauthorizedAccess异常,c#,windows,permissions,C#,Windows,Permissions,我已经编写了一个应用程序(C#,VS2013),并在其中添加了安装(flexera)。 安装过程提供将文件直接复制到ProgramFiles目录并在桌面上创建快捷方式(链接)。 所以,当我单击快捷方式-程序无法启动,并且在windows事件日志中,我看到这些异常: Сббббючбб:系统未经授权的访问例外 System.IO.\uuu错误.WinIOError(Int32,System.String)of System.IO.Directory.InternalCreateDirectory(

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

Сббббючбб:系统未经授权的访问例外 System.IO.\uuu错误.WinIOError(Int32,System.String)of System.IO.Directory.InternalCreateDirectory(System.String, System.String、System.Object、Boolean)of System.IO.Directory.InternalCreateDirectoryHelper(System.String, 布尔值)of main_windows.Settings.Log(System.String)of main_windows.Settings..ctor()of main_windows.Program.main()

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

在那个时候,若我去文件夹和安装的程序,并点击exe文件-它启动,并没有必要启动它作为管理员

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

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

名称空间主窗口
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;
                        }
                    }


                }
            }
        }
    }
}
{ 静态类程序 { [DllImport(“user32.dll”)] [返回:Marshallas(UnmanagedType.Bool)] 静态外部bool setforegroundindow(IntPtr hWnd); /// ///应用程序的主要入口点。 /// [状态线程] 静态void Main() { bool createdNew=true; 使用(互斥体互斥体=新互斥体(true,“Settings”,out createdNew)) { 如果(createdNew) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); 运行(新设置()); } 其他的 { Process current=Process.GetCurrentProcess(); foreach(Process.getProcessByName(current.ProcessName))中的进程 { if(process.Id!=current.Id) { SetForegroundWindow(process.MainWindowHandle); 打破 } } } } } } }
名称空间主窗口
{
静态类程序
{
[DllImport(“user32.dll”)]
[返回:Marshallas(UnmanagedType.Bool)]
静态外部bool setforegroundindow(IntPtr hWnd);
/// 
///应用程序的主要入口点。
/// 
[状态线程]
静态void Main()
{
bool createdNew=true;
使用(互斥体互斥体=新互斥体(true,“Settings”,out createdNew))
{
如果(createdNew)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
运行(新设置());
}
其他的
{
Process current=Process.GetCurrentProcess();
foreach(Process.getProcessByName(current.ProcessName))中的进程
{
if(process.Id!=current.Id)
{
SetForegroundWindow(process.MainWindowHandle);
打破
}
}
}
}
}
}
}

我认为这与应用程序的工作目录有关。请右键单击桌面上的图标并查看“开始位置”文本框中指定的文件夹,好吗?当您从安装文件夹启动程序时,工作目录将是该文件夹本身,因此您的应用程序将工作。工作目录正确(等于安装文件夹),我首先检查它。什么是设置类?因为它在启动过程中试图记录一些内容。你能把你的main_windows.Program.main()方法的代码贴出来吗?是的,可以试试。总的来说,我得到了用于检查应用程序allready运行的互斥锁。还有一些文件。代码不在手上-稍后我会发布它。我认为这并不重要,因为作为管理员和从“发布”运行并没有问题。我认为这与应用程序的工作目录有关。请右键单击桌面上的图标并查看“开始位置”文本框中指定的文件夹,好吗?当您从安装文件夹启动程序时,工作目录将是该文件夹本身,因此您的应用程序将工作。工作目录正确(等于安装文件夹),我首先检查它。什么是设置类?因为它在启动过程中试图记录一些内容。你能把你的main_windows.Program.main()方法的代码贴出来吗?是的,可以试试。总的来说,我得到了用于检查应用程序allready运行的互斥锁。还有一些文件。代码不在手上-稍后我会发布它。我认为这并不重要,因为作为管理员和从“发布”运行没有问题。您试图在Program Files\YourCompany\YourSoftware\logs下创建一个目录,这将失败,是的。您试图在Program Files\YourCompany\YourSoftware\logs下创建一个目录,这将失败,是的。
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();
}