How to open file in a new window? - c#

I want to open folder from web page by clicking on a button ...
In my code when I am use a hyper link its working properly.
but when I am using a button its not working.
here is my code:
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("file://LAP6//C$");
}
Can you provide me some C# code for this functionality.
Thanks

If you simply want to open some folder then you can try this way:
protected void Button1_Click(object sender, EventArgs e)
{
System.Diagnostics.ProcessStartInfo processInfo = new System.Diagnostics.ProcessStartInfo();
processInfo.FileName = "explorer.exe";
processInfo.Arguments = Server.MapPath("YourFolderName");
Response.Write(processInfo.Arguments.ToString());
System.Diagnostics.Process.Start(processInfo);
}
Reply me if it works for you.

Related

Is it possible to add a filename in a link label and have behind the path of the path?

I want to load a file path in a link label control windows forms and display only the the name in the link label but when the label is clicked to open the file from the path.
I did this but I have to display in the link label the entire file path, witch I don't want to.
private void linkPdfLabel__LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Process process = new Process();
process.StartInfo.FileName = linkPdfLabel_.Text;
process.Start();
}
You can consider using one of the 2 solutions. One of which is to use Tag property to assign an absolute file path and use it when clicked.
example code:
void CreateLink(string absoluteFilePath)
{
_linkPdfLabel_.Tag = absoluteFilePath;
_linkPdfLabel_.Text = Path.GetFileName(absoluteFilePath);
}
void linkPdfLabel__LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Process process = new Process();
process.StartInfo.FileName = (string)linkPdfLabel_.Tag;
process.Start();
}
Another way is to use a property called Links which is a LinkCollection which holds a collection of Link objects. You can use this to get the full link instead of the text.
example code:
void CreateFileLink(string absoluteFilePath)
{
linkPdfLabel_.Text = Path.GetFileName(absoluteFilePath);
var link = new LinkLabel.Link();
link.LinkData = absoluteFilePath;
linkPdfLabel_.Links.Add(link);
}
void linkPdfLabel__LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Process process = new Process();
process.StartInfo.FileName = (string)linkPdfLabel_.Link[0].LinkData;
process.Start();
}

Change Path of OpenFileDialog

At the moment I am using this here in my WPF App which works as it should.
private void buttonPresentations_Click(object sender, EventArgs e)
{
openFileDialogPresentations.ShowDialog();
}
It remembers the last path I was in but I want to change it to a set path now.
I have 3 Radiobuttons and each Radiobutton should lead to a different path so I thought about doing it with a variable I give to the openFileDialog but I am not sure on how to go with that. Has anyone done this and can give me a lead on it ?
You can set IntitialDirectory to the folder you want in code where you show the dialog.
private void buttonPresentations_Click(object sender, EventArgs e)
{
openFileDialogPresentations.IntitialDirectory = youFolderPath;
openFileDialogPresentations.ShowDialog();
}
The standard file dialogs have an InitialDirectory property that determines in which folder the dialog opens.
private void buttonPresentations_Click(object sender, EventArgs e)
{
openFileDialogPresentations.InitialDirectory = #"X:\Data\Presentations";
openFileDialogPresentations.ShowDialog();
}
You can do it by using InitialDirectory property. You can set three different paths for radio buttons
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.InitialDirectory=#"D:\MyDir";
dialog.ShowDialog();
}

Button click event clicking exe

I have simple Windows form With just one button which Calls one url
private void button1_Click(object sender, EventArgs e)
{
ProcessStartInfo sInfo = new ProcessStartInfo("http://myurl.com/");
Process.Start(sInfo);
}
When i click exe then it shows button and then i have to click button to perform action. Is it possible to call button click event automatically on clicking exe.
You can do the same with Form.Load event. Just refactor you code with extract method to have no duplicated code and call your function in both event handlers:
private void button1_Click(object sender, EventArgs e)
{
OpenUrl();
}
private void form1_Load(object sender, EventArgs e)
{
OpenUrl();
}
private void OpenUrl()
{
ProcessStartInfo sInfo = new ProcessStartInfo("http://myurl.com/");
Process.Start(sInfo);
}
Simply call:
button1_Click(this, null);
from anywhere in your code.

Using firefox to open c# url button not default browser

I have a very simple program that has 4 buttons each button opens a url.
What i would like is to get each button to use Firefox as the default browser.
I know i can set it as the default browser and then it will work however if a user sets internet explorer as the defualt it will open in that instead.
These url only work in Firefox as it has certificate issues.
So essentially my question is how can i change my code to make sure that if you click on any of the 4 buttons, that it will open up in firefox and not internet explorer.
Here is my code
private void button1_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("http://192.168.2.56/dummy.htm?settings=save&user_active1=on");
BtnOff.BackColor = Color.Red;
Bnton.BackColor = Color.Chartreuse;
}
private void button2_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("http://192.168.2.56/dummy.htm?settings=save&user_active1=off");
Bnton.BackColor = Color.Red;
BtnOff.BackColor = Color.Chartreuse;
}
private void button3_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("https://217.20.xxx.xxx/api/agent/pause.json?active_host=217.20.xxx.xxx&active_user=3012532&username=buildstore&password=cha9pU7U&client=snom");
BtnDND.BackColor = Color.Chartreuse;
BtnQue.BackColor = Color.Red;
}
private void button4_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("https://217.20.xxx.xxx/api/agent/unpause.json?active_host=217.20.xxx.xxx&active_user=3012532&username=buildstore&password=cha9pU7U&client=snom");
BtnQue.BackColor = Color.Chartreuse;
BtnDND.BackColor = Color.Red;
}
Thanks for taking the time to look appreciate it.
you can use
System.Diagnostics.Process.Start(#"C:\Program Files (x86)\Mozilla Firefox\firefox.exe", "http://yoursite.com/dummy.htm");
you can keep the path of firefox.exe in config file instead of hard coding. Alternatively set the PATH environment variable.

Using button click to open helpProvider c#

Just a quick question but is it possible to open a helpProvider?
All I want is open a CHM help file when I click a button in addition to the F1 key?
If it’s not possibly anyone know of a work around?
Thanks Peter
I think you mean a Windows Forms Application.
There is a Windows Forms Control called HelpProvider that does it for you.
System.Windows.Forms.HelpProvider hlpProvider = new System.Windows.Forms.HelpProvider();
hlpProvider.SetShowHelp(this, true);
// Help file
hlpProvider.HelpNamespace = "helpFile.chm";
You can open your help file with
Process proc = new Process();
proc.StartInfo.FileName = "helpFile.cfm";
proc.Start();
private void MainMenu_Load(object sender, EventArgs e)
{
helpProvider1.HelpNamespace = Application.StartupPath + "\\filename.chm";
helpProvider1.SetShowHelp(this, true);
}
private void HelpText_Click(object sender, EventArgs e)
{
Help.ShowHelp(this, helpProvider1.HelpNamespace);
}
Good luck ^_^

Categories