Save user input [duplicate] - c#

This question already has answers here:
Best way to save per user options in C#
(5 answers)
Closed 6 years ago.
So basically,
I have a OpenFileDialog fully working which displays the chosen dir in a textbox.
But I'm wondering how would I save the users input so when they would restart the application it would stay in the textbox? so they wouldn't have to do it over every time.
I understand this might be a stupid question, but I've been googeling some time and found nothing like this. Thanks.

public Form1()
{
InitializeComponent();
if(File.Exists(#"path.txt"))
textBox1.Text = File.ReadAllText(#"path.txt");
}
private void button1_Click(object sender, EventArgs e)
{
if(File.Exists(#"path.txt") == false)
File.Create(#"path.txt");
File.WriteAllText(#"path.txt", textBox1.Text);
}
Just write it in a file, don't forget to include System.IO in your references.

Related

'The system cannot find the file specified' when clicking link [duplicate]

This question already has answers here:
When do we need to set ProcessStartInfo.UseShellExecute to True?
(5 answers)
Closed 2 months ago.
I am following this answer about making hyperlink work in a RichTextBox? by adding this:
private void mRichTextBox_LinkClicked (object sender, LinkClickedEventArgs e) {
System.Diagnostics.Process.Start(e.LinkText);
}
(Actually what I'm really doing is to go to the property of the control, click on the LinkClicked action, and just put the Start() method in there.)
However when clicking the link I get this error:
System.ComponentModel.Win32Exception: 'An error occurred trying to start process 'https://example.com' with working directory 'XYZ'. The system cannot find the file specified.'
Why is that?
If you are using .NET 6 or higher, try this:
Process.Start( new ProcessStartInfo { FileName = e.LinkText , UseShellExecute = true } );

Stop "ding" when pressing Enter? (e.SuppressKeyPress is not recognized)

I've already read through this thread: Stop the 'Ding' when pressing Enter
I'm having no luck with e.SuppressKeyPress. I get an error "'KeyPressEventArgs' does not contain a definition for 'SuppressKeyPress' ..."
I am VERY VERY VERY VERY new to this. I cannot understate this enough. Please explain like I'm 5 years old. Sorry..
I'm using Visual Studio Express 2015 to create a Windows Form with C#.net.
Here is my code:
private void answer_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)ConsoleKey.Enter)
{
CheckAnswer();
}
}
I tried putting e.SuppressKeyPress = true; after my CheckAnswer(); function but I get red squigglies indicating it doesn't know what that is.
Why?
I've had no formal training in using C#.net and I'm missing the foundation of how this is all supposed to work which is messing me up I think. :(

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");
}

Detect app first launch windows phone [duplicate]

This question already has an answer here:
Recognize when is the app first launched WP8
(1 answer)
Closed 8 years ago.
I'm quite new on windows phone dev. I would like to detect when the user launches my app for the first time to display an explaination frame for example by calling :
if(firstLaunch)
showTheFrameToTheGuyBecauseHeLaunchedTheAppForTheFirstTime();
I would be sooooooo glad if someone could show us such a small script...
Thank you in advance guys !
I would recommend you to use the builtin applicationsettings.
const string settingsAppLaunched = "appLaunched";
public static bool IsFirstLaunch(){
IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
return !(settings.Contains(settingsAppLaunched) && settings[settingsAppLaunched]);
}
public static bool Launched(){
if(IsFirstLaunch()){
IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
settings.Add(settingsAppLaunched, true);
settings.Save();
}
}
//usage:
if(IsFirstLaunch()){
showTheFrameToTheGuyBecauseHeLaunchedTheAppForTheFirstTime();
Launched();
}
Microsoft Documentation about Settings in Windows Phone is available here.
You can simply use IsolatedStorageSettings.
if(!IsolatedStorageSettings.ApplicationSettings.Contains("first"))
{
// Do your stuff
IsolatedStorageSettings.ApplicationSettings["first"] = true;
IsolatedStorageSettings.ApplicationSettings.Save();
}
This code will do it.

Write file in C:\ drive permission issue [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
i have developed a win application with c# which generate file in a folder in c:\ drive. when try to generate file there then problem occur for permission issue but when the application generate file in other drive than C:\ then no problem occur. so when i will distribute my apps setup to end user then i wont be sure that user who will install my apps does has the permission to generate file in C:\ drive.
so guide me how can i overcome this issue. should i Using Manifests to Elevate an application in win OS?
i got some article
http://blogs.msdn.com/b/nikhiln/archive/2007/04/19/embed-a-manifest-to-make-an-application-elevate-in-vista.aspx
http://www.codeproject.com/Articles/105506/Getting-Elevated-Privileges-on-Demand-using-C
http://archive.msdn.microsoft.com/KB981778
etc......please guide me with right knowledge. thanks
You can start your application again with elevated permission and have some check at the application's start to see if this is the case. Here's an example: (Be careful not to get into an endless loop of the application starting itself.)
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
string[] args = Environment.GetCommandLineArgs();
if (args.Length > 1 && args[1] == "-e") Text = "Elevated";
}
private void button1_Click(object sender, EventArgs e)
{
Process process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = Application.ExecutablePath,
Arguments = "-e",
Verb = "runas",//-Admin.
}
};
process.Start();
}
}
I agree, though, that storing information in "C:" is probably not a good idea. You can try someplace like: Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData).

Categories

Resources