"System.UnauthorizedAccessException" when extracting file to Desktop - c#

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.

Related

How to get the users certain directory for apps

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.

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();

Compiled HTML Help file shows "This program cannot display...", when pressing F1 on the debugged application

I have this problem on my compiled html help file, which runs on Microsoft Visual Studio 2010.
Note: I don't have enough reputation points to post images. Please bear with my problem.
The name of the compiled html help file is GeneralHelp.chm. In order to make it appear, there are two ways:
Clicking the "General" from "Help" Tab.
Pressing F1 Key when the main form is only active.
I don't modify the default values of the properties, but here are the c# codes for the activation:
private void generalToolStripMenuItem_Click(object sender, EventArgs e)
{
Help.ShowHelp(this, Application.StartupPath + #"\GeneralHelp.chm");
}
private void mdiMain_Load(object sender, EventArgs e)
{
helpProviderGeneral.HelpNamespace = Application.StartupPath + #"\GeneralHelp.chm";
}
The first way is working properly, but the second way (pressing the F1 Key when the main form is only active) is not. It has a message display "This program cannot display the webpage". I tried reconstructing the .chm file, but it still happens.
Furthermore, I found out it becomes normal when I've clicked the other links first, then clicking the page that I would want to see in the navigation pane. My other .chm files doesn't work in this way. I also saved it in the proper folders: Debug and Release. Also, the spelling and case of GeneralHelp.chm is correct. Lastly, when I tried opening the GeneralHelp.chm, outside from MS Visual Studio 2010, it's just normal.
If you need further info, please comment and I'll answer. I just really want to know how this problem be solved. Thanks for the time reading this, I'm looking forward in granting me a solution.
You can imagine helpProvider1.HelpNamespace as a link to the .chm help file. Thus, no help topic on this way can be called. You must have knowledge of the internal structure of the CHM help file as a root node. Think of a structured web page ("homepage") on a web Server with (sub)directories and HTML Topics e.g. #"/Garden/flowers.htm".
As mentioned earlier by other comments (see above) make sure that the .chm file got copied to your EXE project's bin\Debug directory.
In the code example, I've set constants at the beginning. The examples of the CHM Help files depend on how they were compiled by the technical writer e.g. with HTMLHelp Workshop or other Help Authoring tools and are intended here to represent the possibilities.
Please note the inline comments: // set F1 help topic for this form ... // and set F1 help topic for some controls of this form (two lines per control).
namespace C_Sharp_CHM
{
/// <summary>
/// Using C# und CHM files.
/// </summary>
public partial class frmMain : Form
{
private const string sHTMLHelpFileName_ShowSingleHelpWindow = #"\help\CHM-example_ShowSingleHelpWindow.chm";
private const string sHTMLHelpFileName_ShowWithNavigationPane = #"\help\CHM-example_ShowWithNavigationPane.chm";
private const string sHTMLHelpFileName_ShowWithoutAutoSync = #"\help\CHM-example.chm";
public frmMain()
{
InitializeComponent();
}
...
private void frmMain_Load(object sender, EventArgs e)
{
// example: System.Diagnostics.Process.Start(Application.StartupPath + sHTMLHelpFileName_ShowWithoutAutoSync);
webBrowser1.Navigate(new Uri(GetChmUrl(Application.StartupPath + sHTMLHelpFileName_ShowWithoutAutoSync, "Garden/garden.htm")));
if ((chkShowHelpWithNavigationPane.Checked == true))
{
helpProvider1.HelpNamespace = Application.StartupPath + sHTMLHelpFileName_ShowWithoutAutoSync;
}
else
{
helpProvider1.HelpNamespace = Application.StartupPath + sHTMLHelpFileName_ShowSingleHelpWindow;
}
// set F1 help topic for this form
helpProvider1.SetHelpNavigator(this, HelpNavigator.Topic);
helpProvider1.SetHelpKeyword(this, #"index.htm");
// and set F1 help topic for some controls of this form (two lines per control)
helpProvider1.SetHelpNavigator(this.btnPopulate, HelpNavigator.Topic);
helpProvider1.SetHelpKeyword(this.btnPopulate, #"/Garden/flowers.htm");
helpProvider1.SetHelpNavigator(this.btnExit, HelpNavigator.Topic);
helpProvider1.SetHelpKeyword(this.btnExit, #"/Garden/tree.htm");
helpProvider1.SetHelpNavigator(this.chkShowHelpWithNavigationPane, HelpNavigator.Topic);
helpProvider1.SetHelpKeyword(this.chkShowHelpWithNavigationPane, #"/HTMLHelp_Examples/jump_to_anchor.htm#AnchorSample");
helpProvider1.SetHelpNavigator(this.btnOpenHelpShowTopic, HelpNavigator.Topic);
helpProvider1.SetHelpKeyword(this.btnOpenHelpShowTopic, #"/HTMLHelp_Examples/image_and_text.htm");
}
private void btnOpenHelpShowTopic_Click(object sender, EventArgs e)
{
Help.ShowHelp(this.btnOpenHelpShowTopic, helpProvider1.HelpNamespace, HelpNavigator.Topic, #"/HTMLHelp_Examples/image_and_text.htm");
}

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.

Where are these settings coming from?

I have to pretty simple pieces of code:
private void frmMain_Load(object sender, EventArgs e)
{
string[] EmployeeNames = Settings.Default.Employee_Names.Split(new char[] {';'}, StringSplitOptions.RemoveEmptyEntries);
employee_NameComboBox.Items.AddRange(EmployeeNames);
}
And
private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
{
string Employee_Names = string.Empty;
foreach (string Name in employee_NameComboBox.Items)
{
Employee_Names += Name + ";";
}
if (Employee_Names.Length > 1)
{
Settings.Default.Employee_Names = Employee_Names.Substring(0, Employee_Names.Length - 1);
Settings.Default.Save();
}
}
So, to test my code I added the values "Foo;Bar"
To my Settings.Settings in the VS Designer
but when I run the Code Nothing comes up. So I add this code:
private void AddName()
{
if (!employee_NameComboBox.Items.Contains(employee_NameComboBox.Text))
{
employee_NameComboBox.Items.Add(employee_NameComboBox.Text);
}
}
And then I call it on the Leave event of employee_NameComboBox:
private void employee_NameComboBox_Leave(object sender, EventArgs e)
{
AddName();
}
To test this I enter the employee_NameComboBox and type in "Employee1" and leave the CB then enter again and type in "Employee2" and Leave now when I click on the drop down I have these strings in the Items Collection. Great!
But when I close the application and then goto my settings.settings under properties in the Solution Explorer the values "Foo;Bar" are still there.
Continue to start Debugging again and when I click the drop down, the values are "Employee1" and "Employee2". In turn I search through the Solutions Directory in Windows Explorer to try and find the new settings file and I simply CAN NOT find ANY files with "Employee1" or "Employee2" all I see in ANY files is "Foo;Bar".
How can this be? I am saving to somewhere but where!
Also Why doesn't the debugger use the values in the settings.settings or App.Config files?!?!?!
Note: I also tried doing a Find from Visual Studios and Searched Entire Solution for the value Employee1 to no avail.
My end result is that I would like to send out an application with editable combobox that can be saved and edited without a Re-compile.
When such setting are changed at run-time, the values are stored in the following directory:
C:\Documents and Settings\<username>\Local Settings\Application Data
That's where the app then goes and looks for them the next time.
The settings are overwritten on each build by whatever you have set up in VS. If you want to use the settings from a previous compilation (or once deployed to a client machine, a previous version after an update) you have to use Settings.Upgrade(). This should be called once per runtime, most easily in the constructor. See this other StackOverflow question. C# .NET Application Settings and Upgrading

Categories

Resources