How to open text file (.log, .txt etc) in notepad from aspnet

G

Guest

Hi expert,
I have a small web application in C# and ASP.Net and I want to open a file
in notepad when clicked on a linked button. Here is hander code when clicked
on the link button. It can open a process of notepad on the server , but
does not open the file in notepad. What am I doing wrong? can you show me how
to do it please? What I am doing here is : I have datagrid data and when
clicked on linkbutton (logFile), it finds the row identifier and the n
check if any log file exists for that row. If exists, then it should open log
file for that row. When cliked on the link button, it should execute the
ItemClick eventHander.

private void ItemClick(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
//if (((LinkButton)e.CommandSource).CommandName == "SelectItem")
//if (((LinkButton)e.CommandSource).CommandName == "GetLogFile")
try
{

//
//get the row index
int intIndex = (int)dgJobname.DataKeys[e.Item.ItemIndex];
string jobID = ((TextBox)e.Item.Cells[0].Controls[0]).Text;
dvJobStep.RowFilter = "job_id = '" + jobID + "'";


foreach( DataRowView drv in dvJobStep)
{


if (drv["output_file_name"].ToString() != null)
outfile = drv["output_file_name"].ToString();
outfile = outfile.Replace(":","$");
}
// Get the UNC file path
correctfile = @"\\" + Session["ServerName"] + @"\" + outfile;
//checking correct file path
lblError.Text = correctfile;

Process proc = new Process();
//ProcessStartInfo oStartInfo = new ProcessStartInfo();

proc.StartInfo.FileName = "notep ad.exe";
proc.StartInfo.Arguments = correctfile;
//notePad.StartInfo.Arguments = outfile;

proc.Start();
//dgJobname.DataBind();
}
catch(Exception ex)
{
Response.Write(ex.Message);
}
//dgJobname.DataBind();
//Response.Write(outfile);


}


Thank you so much for your help.

Rabindra
 
J

John Timney \(ASP.NET MVP\)

Other than the apparent misspelling of notepad ( "notep ad.exe";) as the
process start argument, the culprit would likely be an inability to spawn
window based applications on the web server without usually having to go
through hoops.

I'm a bit puzzled as to why you would want to do this, all notepad can give
you is text output, and you can do that with three simple lines of code

// create a writer and open the file
TextWriter tw = new StreamWriter("date.txt");

// write a line of text to the file
tw.WriteLine(DateTime.Now);

// close the stream
tw.Close();

--
Regards

John Timney
ASP.NET MVP
Microsoft Regional Director
 
T

TDAVISJR

Besides that, if Notepad is launched it will be launched on the server, not
on the client, since no asp.net code is ran on the client. The only thing
that is sent back is generated HTML output.

--
TDAVISJR
aka - Tampa.NET Koder


John Timney (ASP.NET MVP) said:
Other than the apparent misspelling of notepad ( "notep ad.exe";) as the
process start argument, the culprit would likely be an inability to spawn
window based applications on the web server without usually having to go
through hoops.

I'm a bit puzzled as to why you would want to do this, all notepad can
give you is text output, and you can do that with three simple lines of
code

// create a writer and open the file
TextWriter tw = new StreamWriter("date.txt");

// write a line of text to the file
tw.WriteLine(DateTime.Now);

// close the stream
tw.Close();

--
Regards

John Timney
ASP.NET MVP
Microsoft Regional Director

RN Das said:
Hi expert,
I have a small web application in C# and ASP.Net and I want to open a
file
in notepad when clicked on a linked button. Here is hander code when
clicked
on the link button. It can open a process of notepad on the server , but
does not open the file in notepad. What am I doing wrong? can you show me
how
to do it please? What I am doing here is : I have datagrid data and when
clicked on linkbutton (logFile), it finds the row identifier and the n
check if any log file exists for that row. If exists, then it should open
log
file for that row. When cliked on the link button, it should execute the
ItemClick eventHander.

private void ItemClick(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
//if (((LinkButton)e.CommandSource).CommandName == "SelectItem")
//if (((LinkButton)e.CommandSource).CommandName == "GetLogFile")
try
{

//
//get the row index
int intIndex = (int)dgJobname.DataKeys[e.Item.ItemIndex];
string jobID = ((TextBox)e.Item.Cells[0].Controls[0]).Text;
dvJobStep.RowFilter = "job_id = '" + jobID + "'";


foreach( DataRowView drv in dvJobStep)
{


if (drv["output_file_name"].ToString() != null)
outfile = drv["output_file_name"].ToString();
outfile = outfile.Replace(":","$");
}
// Get the UNC file path
correctfile = @"\\" + Session["ServerName"] + @"\" + outfile;
//checking correct file path
lblError.Text = correctfile;

Process proc = new Process();
//ProcessStartInfo oStartInfo = new ProcessStartInfo();

proc.StartInfo.FileName = "notep ad.exe";
proc.StartInfo.Arguments = correctfile;
//notePad.StartInfo.Arguments = outfile;

proc.Start();
//dgJobname.DataBind();
}
catch(Exception ex)
{
Response.Write(ex.Message);
}
//dgJobname.DataBind();
//Response.Write(outfile);


}


Thank you so much for your help.

Rabindra
 
P

Patrice

What would you do once Notepad is opened on the *server* ? (don't you want
to open a text file client side ?)

You may want to explain what you are trying to do so that someone can
suggest perhaps another approach....

Patrice
 
Joined
May 22, 2008
Messages
2
Reaction score
0
Process.Start() is not working

Process.Start() is not working when we are deploying the application on IIS server or any other live server.
 
Joined
May 22, 2008
Messages
2
Reaction score
0
Use Response.Redirect()

Its better to use Response.Redirect("request.txt") i.e opening a text file in the browser ... and the user can save that notepad file on the client computer using the Save As option of the browser.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,120
Latest member
ShelaWalli
Top