Open File With C# / Asp.net

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • stefbek97
    New Member
    • Aug 2009
    • 54

    Open File With C# / Asp.net

    Hello All,
    I am having a bit of trouble opening a file in my ASP.Net/C# Application.
    I have a file Path and name stored in my Database. I display that filename in a Gridview control. I also have a Button next to the File name that i use to open the file in notepad. Here is that code.

    Code:
        protected void gvHistoryRecent_SelectedIndexChanged(object sender, EventArgs e)
        {
            GridViewRow row = gvEquipmentHistoryRecent.SelectedRow;
    
            Datalink = row.Cells[12].Text;
    
            System.Diagnostics.Process.Start(@"C:\windows\system32\notepad.exe", Datalink);
        }
    as you can see I am using the SelectIndexChan ged Event to get the file name of the current row selected.
    Now this works great on my Local application and having the file itself on my Local PC. But when I have a file stored on the Server and the application residing on the server I am unable to open the file. Notepad will not even open.
    Any Ideas?
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    have you confirmed that notepad really exists at that assumed location on the server?

    If its a 64-bit OS then that is not where Notepad resides.
    I am unsure about the location of Notepad in something like WinServer2003/5/8

    Comment

    • stefbek97
      New Member
      • Aug 2009
      • 54

      #3
      Thanks for the reply. I did check and notepad does reside at that location. Could that be the only reason notepad does not open.?

      Comment

      • GaryTexmo
        Recognized Expert Top Contributor
        • Jul 2009
        • 1501

        #4
        Which OS is the server and the local machine, and does notepad give any errors when it fails? If it's one of the newer operating systems, I'm wondering if it has any issues running from the windows directory. An example of what I mean here is that I'm used to windows XP, but when I work on windows 2008 servers at work I get messed up by the whole elevated privileges thing. It drives me nuts how I have to run programs with admin privileges, even if the account is supposedly an administrator. I know it's for improved security, but it's still annoying :)

        Anyway, do you think it could be something like that?

        Comment

        • stefbek97
          New Member
          • Aug 2009
          • 54

          #5
          Hi GaryTexmo,
          That could be. My local Machine has Windows XP. I need to check what OS the server is using. No errors from Notepad just never opens up.
          Thanks in advance

          Comment

          • tlhintoq
            Recognized Expert Specialist
            • Mar 2008
            • 3532

            #6
            Another angle on this is "do you have to specify notepad?"
            If it's a .txt file then the OS already has a default application for it.
            Might be notepad. Might be Wordpad. What about just opening the document and letting the OS worry about what application to do it in?

            Comment

            • stefbek97
              New Member
              • Aug 2009
              • 54

              #7
              What would the code look like to just open the document?'

              Comment

              • tlhintoq
                Recognized Expert Specialist
                • Mar 2008
                • 3532

                #8
                Originally posted by stefbek97
                What would the code look like to just open the document?'
                Got me. It was an idea that I thought might be worth researching.

                Comment

                • cloud255
                  Recognized Expert Contributor
                  • Jun 2008
                  • 427

                  #9
                  HI,

                  The code to tell the Windows to open a file using its default file associations is:

                  Code:
                  Process.Start(@"C:\Document.txt");
                  But remember that this code executes on the server, so if you want notepad to open on the client machine you will need to use client side code i.e. javascript, the above line of code will open notepad (or whatever application is associated with .txt files) on the server.

                  Hope this helps

                  Comment

                  • stefbek97
                    New Member
                    • Aug 2009
                    • 54

                    #10
                    Thanks I will Try It.

                    Comment

                    • tlhintoq
                      Recognized Expert Specialist
                      • Mar 2008
                      • 3532

                      #11
                      Cool. Thanks. I love learning new stuff from other people's questions

                      Comment

                      • stefbek97
                        New Member
                        • Aug 2009
                        • 54

                        #12
                        The Code still does not seem to open the file or notepad.

                        Comment

                        • tlhintoq
                          Recognized Expert Specialist
                          • Mar 2008
                          • 3532

                          #13
                          Originally posted by stefbek97
                          The Code still does not seem to open the file or notepad.
                          But when I have a file stored on the Server and the application residing on the server I am unable to open the file. Notepad will not even open.
                          As Gary pointed out, Notepad is going to open on the server not on a client PC.
                          You are checking that it is not opening on the server, right?
                          If so, then I would first suspect that the path you are giving it for the text file is not valid FROM THE PERSPECTIVE of the server.

                          The file path is stored in your variable "Datalink". We have no idea what that path is.
                          If it is "Z:\\MyFile " because it is a mapped drive on a client PC and the server has no "Z:" drive, then the path is invalid to the server.

                          Comment

                          • stefbek97
                            New Member
                            • Aug 2009
                            • 54

                            #14
                            I checked that. The mapped path on my machine is Z: the server is D: and the filename is D:\XXXXXX. Maybe a work around would be to read the contents of the file and display in a Textbox or a Pop up. Would that be possible?

                            Comment

                            • stefbek97
                              New Member
                              • Aug 2009
                              • 54

                              #15
                              Ok so after Playing around I am able to Load contents of the file into a Textbox. But it comes back all in one line. Also if i wanted to modify and open up in a new window would that be possible?

                              Code:
                              protected void gvHistoryRecent_SelectedIndexChanged(object sender, EventArgs e)
                                  {
                                      GridViewRow row = gvEquipmentHistoryRecent.SelectedRow;
                              
                                      Datalink = row.Cells[12].Text;
                              
                                      StreamReader strRdr = new StreamReader(Datalink + ".txt");
                                      txtDoc.Text = strRdr.ReadToEnd();
                                
                                  }
                              Last edited by tlhintoq; Aug 20 '09, 10:03 PM. Reason: [CODE] ...your code goes here... [/CODE] tags added

                              Comment

                              Working...