ASP.NET - How to open a text file situated at server

Asked By Sreenath G V on 02-May-11 05:09 AM

Hi 

I have one text file in server. I need to open .doc or .txt file Please Suggest me if you  have any ideas. I am using ..

System.Diagnostics.Process.Start(“PATH”);

 

  Thanks

Sahil Kumar replied to Sreenath G V on 02-May-11 05:15 AM

Hi,

It does not matter whether file is situated locally or on remote server. If you have permission to access that you can access it easily.

For example remote path is : \\192.168.10.10\C:\somefiles\1.txt

Now you have to remove : and use $

So use this

String newpath=path.Replace(':', '$');

And you can now read file using stream reader object easily. or you can start will process object also.

StreamReader reader = new StreamReader(newpath, Encoding.Default);

The solution is for txt file not for doc. I hope this will help you.

Ravi S replied to Sreenath G V on 02-May-11 05:15 AM
HI

use this code

<%@ Import Namespace="System.IO" %>
<script language="vb" runat="server">

public void Page_Load(object sender, EventArgs e)
{
	//Open a file for reading
	string FILENAME = Server.MapPath("Rand.txt");
	//Get a StreamReader class that can be used to read the file
	StreamReader objStreamReader = default(StreamReader);
	objStreamReader = File.OpenText(FILENAME);
	//Now, read the entire file into a string
	string contents = objStreamReader.ReadToEnd();
	//Set the text of the file to a Web control
	lblRawOutput.Text = contents;
	//We may wish to replace carraige returns with <br>s
	lblNicerOutput.Text = contents.Replace(Constants.vbCrLf, "<br>");
	objStreamReader.Close();
}
</script>

<b>Raw File Output</b><br />
<asp:label runat="server" id="lblRawOutput" />
<p>
<b>Nicer Output</b><br />
<asp:label runat="server" id="lblNicerOutput" Font-Name="Verdana" />








hope it helps you...
Reena Jain replied to Sreenath G V on 02-May-11 05:18 AM
hi,

To open file create instance of http://msdn2.microsoft.com/en-us/library/system.io.filestream.aspx class with http://msdn2.microsoft.com/en-us/library/system.io.filemode.aspx and http://msdn2.microsoft.com/en-us/library/4z36sx0f.aspx enumerations as parameters. It's important to always close the stream. If you don't close the stream it can take a minute to be file again accessible (it will wait to garbage collector to free the FileStream instance and close the file).

using System.IO;
 
FileStream fileStream = new FileStream(@"c:\file.txt", FileMode.Open);
try
{
  // read from file or write to file
}
finally
{
  fileStream.Close();
}

hope this will help you
Riley K replied to Sreenath G V on 02-May-11 05:18 AM
Use this below code

 
url = "http:\\website\dir\sample.txt";
 
  
 
Uri uri = new Uri(url);
 
  
 
//Create the request object
 
WebRequest req = WebRequest.Create(uri);
 
WebResponse resp = req.GetResponse();
 
Stream stream = resp.GetResponseStream();
 
StreamReader sr = new StreamReader(stream);
  
string s = sr.ReadToEnd();
TSN ... replied to Sreenath G V on 02-May-11 05:18 AM
HI..

YOu can Use FtpWebRequest for reading text file on remote server...

here follows the example..

private void Download(string filePath, string fileName)
{
  FtpWebRequest reqFTP;
  try
  {
    //filePath = <<The full path where the file is to be created. the>>,
    //fileName = <<Name of the file to be createdNeed not
    //name on FTP server. name name()>>
    FileStream outputStream = new FileStream(filePath +
                "\\" + fileName, FileMode.Create);
    reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" +
                ftpServerIP + "/" + fileName));
    reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
    reqFTP.UseBinary = true;
    reqFTP.Credentials = new NetworkCredential(ftpUserID,
                          ftpPassword);
    FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
    Stream ftpStream = response.GetResponseStream();
    long cl = response.ContentLength;
    int bufferSize = 2048;
    int readCount;
    byte[] buffer = new byte;
    readCount = ftpStream.Read(buffer, 0, bufferSize);
    while (readCount > 0)
    {
      outputStream.Write(buffer, 0, readCount);
      readCount = ftpStream.Read(buffer, 0, bufferSize);
    }
    ftpStream.Close();
    outputStream.Close();
    response.Close();
  }
  catch (Exception ex)
  {
    MessageBox.Show(ex.Message);
  }
}
 hope tis helps you...
Sreenath G V replied to Sahil Kumar on 02-May-11 05:19 AM
Hi Actually I am using path as c:\Myfolder\sample.txt. This is actully in server. And if I do

System.Diagnostics.Process.Start(“c:\Myfolder\sample.txt”)...it wont open right..?

Sahil Kumar replied to Sreenath G V on 02-May-11 05:23 AM
Hi,

You need to provide servername also

System.Diagnostics.Process.Start(“server name\c$\Myfolder\sample.txt”)

Then it will work for you but you got to have admin previliges for it.
Sahil Kumar replied to Sreenath G V on 02-May-11 05:26 AM
Hi,

Another thing you can do is if you dont have admin previliges is have a shared folder keep file into that and just give its

abosolute path and try to access it will work for you.

Thanks
help
you would need to use the ItemActivate event of the ListView and simply use the Process.Start method on the url value. Sample snippet is provided below: private void myListView_ItemActivate(object sender sender; string selectedURL = lv .SelectedItems[0].Tag.ToString(); try { if (!String.IsNullOrEmpty( selectedURL )) System.Diagnostics.Process.Start(selectedURL); } catch(Exception ex) { / / handle the exception that occurs. } } Hope I understood you right. Sorry ContentType = "application / x-www-form-urlencoded"; request.Timeout = 15000; response = (HttpWebResponse)request.GetResponse(); ret = new StreamReader(response.GetResponseStream(), Encoding.Default).ReadToEnd(); response.Close(); } catch { } finally { if (response ! = null) { response.Close(); } } return ret; } #endregion Depending on of response in the stream, you may need to use a BinaryReader instead of the StreamReader. Ty Robbe! keywords: HttpWebResponseHttpWebRequest, IsNullOrEmpty, ListViewItem, GetResponseStream, BinaryReader description: Execute the download Execute the download
result = p.HtmlToPdfConvertFile(htmlURL, pdfFile); if (result = = 0) { System. Console .WriteLine( "Converted successfully!" ); System.Diagnostics. Process .Start(pdfFile); } else System. Console .WriteLine( "Converting Error!" ); } } For iTextSharp : http: / / blog.dmbcllc.com / 2009 / 07 LoadTagStyle( "body" , "font-size" , "12px" ); document.NewPage(); objects = iTextSharp.text.html.simpleparser. HTMLWorker.ParseToList( new StreamReader(strHTMLpath, Encoding.Default), styles); for ( int k = 0; k < objects.Count; k++) { document.Add((IElement)objects[k]); } / / Clear
Hi. When I use Process.Start("cmd.exe", "ping 127.0.0.1") or Process.Start("cmd.exe", " / C ping 127.0.0.1") it open up the command prompt and WorkingDirectory property to C: \ Windows \ system32 \ but I still won't work. Kindly advise. Thanks. Process.Start("cmd.exe", "C: \ Windows \ System32 \ ping 127.0.0.1") Actually, all I want is
Hi All, I am opening a .csv file in excel using the System.Diagnostics.Process.Start(sPath + " \ " + sTempFilename); I am using it from a form whose topmost property is set to excel file on top of all the forms. You cannot open file using System.Diagnostics.Process.Start(sPath + " \ " + sTempFilename); The argument should be executable file. You can use System.Diagnostics.Process.Start("cmd.exe" , " / c " + sPath + " \ " + sTempFilename); this means like you are opening this file from command
Hi I have started an (Exe) using System.Diagnostics.Process.Start(EXE) . I need a logic for knowing the exact time(intreval) that my process spanned can perform my next step on the Exe , Which includes Sending keystrokes Instance = System.Diagnostics.Process.Start(EXE) While Instance.WaitForInputIdle() System.Threading.Thread.Sleep(1000) End While SendKeys.SendWait( "{ESC}" ) ' I base .ProcessDialogKey(keyData); } keywords: WaitForInputIdle, VB.NET, bool, SendKeys, Console, Thread, Tab description: System.Diagnostics.Process.Start Hi I have started an (Exe) using System.Diagnostics.Process.Start(EXE) . I need a
Hi, I am getting a sstem Exception while executing the below lines of code. Process objProcess = Process.Start(objProcessStartInfo); / / objProcess.WaitForExit(); Background: I am trying to execute a batch file in an upgrade Windows 7 - Jap. . . . but where as it is not working in Windows 7 Eng. The Process.start() method returns FALSE. Thanks in advance Regards, Naveen try { Process objProcess = Process.Start(objProcessStartInfo); } catch (Exception ex) { / / put a breakpoint on the next line System.Diagnostics.Debug.WriteLine
Hi, I would like to strat to open outlook to start an email. I can open outlook using the code Process.Start("Outlook.exe"), but I would like to open a new email page ready to send line". Answer is as follows. . . string _email = "mailto:"+"email address ( or varible containing email address)"; Process.Start(_email); Regards Jez Try this - System.Diagnostics.Process.Start("Outlook.exe" , " / c ipm.note / m tester@testsky.com"); Explore "Command-line switches" under Outlook
Hi I have started an (Exe) using System.Diagnostics.Process.Start(EXEPath) . I need some mechanism logic so that i know the exact time that my i perform my next step on the Exe .Which includes Sending keystrokes Instance = System.Diagnostics.Process.Start(EXEPath) While Instance.WaitForInputIdle() System.Threading.Thread.Sleep(1000) End While SendKeys.SendWait( "{ESC}" ) ' I ImageView_Fullscreen " + fileName.TrimEnd (null)); try { / / Pass the ProcessStartInfo object to the Start function. System.Diagnostics.Process.Start (f); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine (ex.ToString ()); } This is my most recent