How to get the users certain directory for apps - c#

I am trying to make it so it finds the current user on the pc so that when i put (for example USERPROFILE) it can find the right directory to the exe
private void siticoneImageButton3_Click_1(object sender, EventArgs e)
{
Process.Start("C:\\Users\\USERPROFILE\\AppData\\Local\\Programs\\lunarclient\\Lunar Client.exe");
}

You should be able to use the Environment.UserName property like so:
Process.Start($"C:\\Users\\{Environment.UserName}\\AppData\\Local\\Programs\\lunarclient\\Lunar Client.exe");

You could try using Environment.UserName or Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) to get the path to (...)\AppData\Local directory.

Related

"System.UnauthorizedAccessException" when extracting file to Desktop

I created a script to extract an executable file from the Resources to my Desktop. This works on my machine, but won't work on other's people machines because of a different username. The following script works perfectly:
private void button1_Click(object sender, EventArgs e)
{
byte[] myfile = Properties.Resources.SOMETHING;
File.WriteAllBytes("C:\\Users\\Alex\\Desktop\\SOMETHING.exe",myfile);
}
I did some some research and found that I need to use the
(Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
So I compiled this script:
private void button1_Click(object sender, EventArgs e)
{
byte[] myfile = Properties.Resources.SOMETHING;
File.WriteAllBytes(Environment.GetFolderPath(Environment.SpecialFolder.Desktop),myfile);
}
The problem is that it doesn't say there is an error in my code, but when I run it and press on the button I get the following error:
System.UnauthorizedAccessException Message=Access to the path 'C:\Users\Alex\Desktop' is denied.
I've tried running the code with administrator rights, but that also didn't help.
Use File.SetAttributes(myfile, FileAttributes.Normal); property before reading the file, it should work.

Making a file path dynamic

I just want to Code an Launcher for the Game i currently making, but i ran into an Problem and i am an absolutely beginner as a Programmer.
Currently i have a Start Button with looks like this
private void startbtn_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("C:\\Users\\Windows\\Desktop\\Folder\\Underfolder\\Game.exe");
}
But i try to make it Dynamic, so the Launcher can work which ever the User install the Data (There is an Installer for everything) also the launcher is located in the same directory as the Game.exe
(The Code looks weird on the Post but it is correct)
The most reliable way I've found to do this is (assuming Game.exe is located in the same path as your Launcher.exe as you mentioned in your post):
var launcherExeDirectory = AppDomain.CurrentDomain.BaseDirectory;
var gameExeFullPath = Path.Combine(launcherExeDirectory, "Game.exe");
Then you can just do something like:
Process.Start(gameExeFullPath);
Just use GetCurrentDirectory
Process.Start(System.IO.Directory.GetCurrentDirectory + #"\game.exe");
///Or
Process.Start(AppDomain.CurrentDomain.BaseDirectory + #"\game.exe");
///or
Process.Start(Application.ResourceAssembly.Location + #"\game.exe");
///or
Process.Start( System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + #"\game.exe");

Run a Explorer / Folderbrowserdialog with another users permission

I have an application that should be able to run explicit tasks in another users context, so that within the application, a less privileged user is able to do some tasks, he is not allowed to.
I used for this an impersonation and it works fine with the acutal code, but I can not make it work with a Folderbrowser Dialog. I think the browser is executed within the context of the correct user, but uses other windows functions which override the user context.
My code that does not work is:
private void tb_customRoot_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
ImpersonationHelper.Impersonate("STARK", VSSFileExplorer.Properties.Settings.Default.FS_User, BASE64.Base64Decode(VSSFileExplorer.Properties.Settings.Default.FS_Password), delegate
{
VistaFolderBrowserDialog myFancyFolderDialog = new VistaFolderBrowserDialog();
DirectoryInfo rootDir = new DirectoryInfo(#"C:\");
try
{
rootDir = new DirectoryInfo(VSSFileExplorer.Properties.Settings.Default.CustomRoot);
}
catch
{ throw; }
myFancyFolderDialog.SelectedPath = rootDir.ToString();
myFancyFolderDialog.ShowDialog();
tb_customRoot.Text = myFancyFolderDialog.SelectedPath;
});
}
The problem is, that after opening the browser dialog, windows opens a "Enter network credentials" login prompt. The user should not know this credentials.
Is there a way to run a Folderbrowserdialog with another users rights?
I also build a function which will generate the correct path from some Tools in the GUI, but I am really interested if this is possible.
Thanks in advance.

Launching a Windows 10 Store app from C# executable

So here's the gist of my problem: I have a keyboard where I can assign macros and/or launch programs from. I want to include a couple Win10 and Steam applications in that list. So I opted to build an executable "launcher", so to speak.
The code is simplistic in nature. I got Steam url's to work by placing the steam url into Process.Start("steam://rungameid/#####"). I cannot, however, figure out how to get Win10 apps to work. Here's my class:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Process.Start(#"explorer.exe shell:AppsFolder\4DF9E0F8.Netflix_mcm4njqhnhss8!Netflix.App");
Process.Start(#"shell:AppsFolder\4DF9E0F8.Netflix_mcm4njqhnhss8!Netflix.App");
Process.Start("netflix://");
Application.Exit();
}
}
Each line of Process.Start() is what I've tried, to no avail.
The bottom line I attempted from this answer, which also did not work
The first line, I can put that in a Run box or from the command line saDand it will launch Netflix, but from the C# application, I get a "System cannot find the file" exception.
Thanks for any direction!
Can you please check if you have installed this app and name you enter in the Process.Start(“ ”) is correct, You can find the names when you open the registry key HKEY_CLASSES_ROOT\Extensions\ContractId\Windows.Protocol\PackageId. Look for the CustomProperties key. It has an attribute Name. I use the below sample to open my photos, It works fine.
private void Form4_Load(object sender, EventArgs e)
{
button2_Click(null,null);
}
private void button2_Click(object sender, EventArgs e)
{
Process.Start("ms-photos://");
}
Instead of
Process.Start(
#"explorer.exe shell:AppsFolder\4DF9E0F8.Netflix_mcm4njqhnhss8!Netflix.App"
);
Do this
Process.Start(
"explorer.exe",
"shell:AppsFolder\4DF9E0F8.Netflix_mcm4njqhnhss8!Netflix.App"
);
I was having the same problem. Currently unable to launch a windows app store application from c#. I used a work around for now. I made a bat file that navigates to the desktop and launches the desktop shortcut link. Then I call my bat file which launches the app store application.
Example of BAT file:
cd\
cd Users\d1\OneDrive\Desktop
"XYZ Games - Shortcut.lnk"
Example Code C#:
Process proc = new Process();
proc.StartInfo.FileName = "launcherXYZGames.bat";
proc.Start();

Load google map failed

In my a c# program, I want to load a google map which is a html file stored in my local disk but it failed.It says that the application have something wrong with internet connection. Here is my screenshot and some code
private void 加载地图ToolStripMenuItem_Click(object sender, EventArgs e)
{
//webBrowser1.Navigate("D:/GoogleMap/GoogleMap.html");
webBrowser1.Navigate("D:/exercise/源代码/windows监控端/TestGoogleMapGps/TestGoogleMapGps/GoogleMap.html");
}
Well normally your local machine uses the backslash, not the forward slash, so try:
webBrowser1.Navigate(#"D:\GoogleMap\GoogleMap.html");
webBrowser1.Navigate(#"D:\exercise\源代码\windows监控端\TestGoogleMapGps\TestGoogleMapGps\GoogleMap.html");
Although, I'm not entirely sure that's the solution you're going for.

Categories

Resources