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

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

Related

How to create a file relative to Program.cs [duplicate]

This question already has answers here:
Get the application's path
(21 answers)
Closed 3 months ago.
The community reviewed whether to reopen this question 3 months ago and left it closed:
Original close reason(s) were not resolved
I want to create a file relative to Program.cs. This code in case of VSCode works correct:
string myFile1 = #".\temp1.txt";
File.Create(myFile1);
string myFile2 = "./temp2.txt";
File.Create(myFile2);
but Visual Studio IDE 2022 creates file in:
`MyProject\bin\Debug\net6.0`
Is there any universal solution?
You can include the following method anywhere within your project:
using System.Runtime.CompilerServices
public static string GetSourceFilePathName( [CallerFilePath] string? callerFilePath = null )
=> callerFilePath ?? "";
Then, you can invoke that method from your Program.cs, and it will give you C:\Users\YOU\Documents\Projects\MyProject\Program.cs.
I suppose you know what to do from there.

ContentDialog showAsync() giving error: no definition for getAwaiter [duplicate]

This question already has an answer here:
UWP app: FileOpenPicker PickSingleFileAsync() can't await
(1 answer)
Closed 5 years ago.
I am building an UWP app (C#) and on click of button, I want to verify input.
If verification fails, I want to display a message saying that there is wrong input.
this is my function which should handle this:
private async void AddContact_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e) {
DBHelper db = new DBHelper();
if(i_name.Text != "" && i_phone.Text != "") {
db.insertPerson(new Person(i_name.Text, i_phone.Text));
Frame.Navigate(typeof(MainPage));
} else {
ContentDialog msg = new ContentDialog() {
Title = "Empty input!",
Content = "Please fill both fields!",
CloseButtonText = "OK"
};
await msg.ShowAsync();
}
}
However, when the last part (await msg.showAsync() ) is typed it stays underlined in red (in VS). The builder reads the following error:
Error CS4036 'IAsyncOperation' does not contain a definition for 'GetAwaiter' and no extension method 'GetAwaiter' accepting a first argument of type 'IAsyncOperation' could be found (are you missing a using directive for 'System'?) testniStudioApp d:...\SecondPage.xaml.cs 43 Active
Now i tried to assign this call to variable (as I saw that someone solved similiar problem that way), but it still didn't worked.
Then I tried clearing NuGet cache and rebuilding project as stated here, but none of those solutions worked.
I have a hard time understanding what it wants from me.
Every single tutorial on how to display message was written in more or less the same manner, so I can't understand what can be wrong.
using System; was mising.
Thanks to comments for answer

Save user input [duplicate]

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.

Not able to run batch file from Windows Service Application [duplicate]

This question already has answers here:
Calling a batch file from a windows service
(3 answers)
Closed 9 years ago.
I am having batch file : sample.bat with following code:
#ECHO OFF
SET /a INT1=%1
SET /a INT2=%2
SET /a ANSWER=INT1*INT2
ECHO %ANSWER%
PAUSE
Also created another batch file : cmdSample.bat with following code:
sample 2 4
So if i run cmdSample.bat file, it gives me correct result.
After that i have created 1 windows service application in which i tried to call that batch file and pass the command, as follows:
private void DoWork()
{
try
{
string fname = #"C:\Users\of4\Desktop\sample.bat";
string cmd = "sample 2 4";
RunSampleBatch(fname, cmd);
}
}
private void RunSampleBatch(string fname, string cmd)
{
Process p = new Process();
p.StartInfo.FileName = fname;
p.StartInfo.Arguments = cmd;
p.Start();
}
Can anyone help me, why i am not able to execute batch file through windows service application??
Thanks in advance..
Your parameters are off, you are giving "sample" as your first parameter to the sample.bat.
Your service is probably not running under your user account. Maybe it cannot even access your file.
You need to start your batchfile using cmd.exe. You can find a very good explanation here.
What are you trying to do? Running a batch file from a windows service doesn't make much sense, you won't be able to see the results. Maybe you should try a console application first to debug your problems.
You might also want to post an actual error next time, because all of the above is just guesswork, we need more info than just "doesn't work".

Check if browser closed manually

In visual studio 2010, working with c#;
I open a browser with:
private IE browser;
private void Set_Browser()
{
string splashUrl = "google.com";
browser= new IE(splashUrl);
}
If a user(person) closes the browser by accident, then my application will not be able to work anymore.
QUESTIONS:
So how do I check if a user closed the browser manually?
Can I unable a user from closing the browser by adding the browser to my
application GUI as a control? [using Windows Forms]
-> How do I do that?
Last question related to this post How to use watin with WebBrowser control? (2 years old, but no decent answer too)
EDIT: The solution in give URL seems to work. Problem is that if I try to send the WebBrowser.ActivateX.. as an object to other class. Then my browser = new IE(..) returns null. It does work when I instantiate it in the form class though. Any solutions?
You can search for the process of internet explorer every x seconds and see if the browser is already running using this code:
bool isRunning = false;
foreach (Process clsProcess in Process.GetProcesses()) {
if (clsProcess.ProcessName.Contains("iexplore"))
{
isRunning = true;
break;
}
}
You can use this article
Or you can add a browser control to your application using this article
One thing you can do is hide the browser to avoid users closing it .. See this SO question.
Hiding Internet Explorer when WatiN is run

Categories

Resources