By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,795 Members | 1,216 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,795 IT Pros & Developers. It's quick & easy.

Open File With C# / Asp.net

P: 54
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.

Expand|Select|Wrap|Line Numbers
  1.     protected void gvHistoryRecent_SelectedIndexChanged(object sender, EventArgs e)
  2.     {
  3.         GridViewRow row = gvEquipmentHistoryRecent.SelectedRow;
  4.  
  5.         Datalink = row.Cells[12].Text;
  6.  
  7.         System.Diagnostics.Process.Start(@"C:\windows\system32\notepad.exe", Datalink);
  8.     }
as you can see I am using the SelectIndexChanged 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?
Aug 19 '09 #1
Share this Question
Share on Google+
25 Replies


tlhintoq
Expert 2.5K+
P: 3,525
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
Aug 19 '09 #2

P: 54
Thanks for the reply. I did check and notepad does reside at that location. Could that be the only reason notepad does not open.?
Aug 20 '09 #3

GaryTexmo
Expert 100+
P: 1,501
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?
Aug 20 '09 #4

P: 54
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
Aug 20 '09 #5

tlhintoq
Expert 2.5K+
P: 3,525
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?
Aug 20 '09 #6

P: 54
What would the code look like to just open the document?'
Aug 20 '09 #7

tlhintoq
Expert 2.5K+
P: 3,525
@stefbek97
Got me. It was an idea that I thought might be worth researching.
Aug 20 '09 #8

Expert 100+
P: 427
HI,

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

Expand|Select|Wrap|Line Numbers
  1. 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
Aug 20 '09 #9

P: 54
Thanks I will Try It.
Aug 20 '09 #10

tlhintoq
Expert 2.5K+
P: 3,525
Cool. Thanks. I love learning new stuff from other people's questions
Aug 20 '09 #11

P: 54
The Code still does not seem to open the file or notepad.
Aug 20 '09 #12

tlhintoq
Expert 2.5K+
P: 3,525
@stefbek97
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.
Aug 20 '09 #13

P: 54
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?
Aug 20 '09 #14

P: 54
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?

Expand|Select|Wrap|Line Numbers
  1. protected void gvHistoryRecent_SelectedIndexChanged(object sender, EventArgs e)
  2.     {
  3.         GridViewRow row = gvEquipmentHistoryRecent.SelectedRow;
  4.  
  5.         Datalink = row.Cells[12].Text;
  6.  
  7.         StreamReader strRdr = new StreamReader(Datalink + ".txt");
  8.         txtDoc.Text = strRdr.ReadToEnd();
  9.  
  10.     }
Aug 20 '09 #15

tlhintoq
Expert 2.5K+
P: 3,525
The mapped path on my machine is Z: the server is D: and the filename is D:\XXXXXX.
But what is the PATH you are sending to try to open?
Z:\xxxxxx.txt
or
D:\xxxxxx.txt

If you are sending Z:\xxxxx.txt that doesn't automatically translate to D:\xxxxx.txt for the server. The server will try to open "Z:\xxxxx.txt" which doesn't exist as the server doesn't have a Z: drive, it has a D: drive.
Aug 20 '09 #16

tlhintoq
Expert 2.5K+
P: 3,525
TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.
Aug 20 '09 #17

tlhintoq
Expert 2.5K+
P: 3,525
Well... You found the problem with the path... Didn't you?
Expand|Select|Wrap|Line Numbers
  1. System.Diagnostics.Process.Start(@"C:\windows\system32\notepad.exe", Datalink);
versus
Expand|Select|Wrap|Line Numbers
  1. StreamReader strRdr = new StreamReader(Datalink + ".txt");
When trying to open it in notepad your code wasn't adding the extension but as a streamreader it did.

You should be able to do as you originally intended now.

Let me make one suggestion. Instead of blindly added an extension, change the extension. If there already is a .txt you won't get filename.txt.txt If there isn't an extension it will be added

Expand|Select|Wrap|Line Numbers
  1. Datalink = System.IO.Path.ChangeExtension(Datalink, "txt")
;
Aug 20 '09 #18

P: 54
The DataLink variable contains the same path as the server since when i uploadload the file it uses Server.Mappath .I also store the filename in my database and it is being saved as drive D:. The file is definitely in the correct location since i am able to display the content in a textbox.
Aug 20 '09 #19

P: 54
That was not the issue. I tried adding the .txt Extension while i was fiddling around. But it still will not open the file from word or notepad
Aug 20 '09 #20

tlhintoq
Expert 2.5K+
P: 3,525
If you just walk up to the server and open notepad, does it open?
Aug 20 '09 #21

P: 54
Yes it does. I also Checked and made sure the location matched C:\windows\system32\notepad.exe.
Aug 20 '09 #22

tlhintoq
Expert 2.5K+
P: 3,525
From the Run prompt you should be able to just enter the file path and have it open in the default application. Essentially doing the same as your "process.start"
Aug 20 '09 #23

P: 54
I will try again tomorrow since I am away from the server now. Maybe any ideas on writing file content to textbox?
Aug 20 '09 #24

tlhintoq
Expert 2.5K+
P: 3,525
@stefbek97
There are lots of "how to write a file" tutorials out there. Take a look on the MSDN for TextWriter

I just hate to see you gerryrig something that you hadn't intended because of the original issue. It doesn't solve the issue, it side-steps it. Meaning that if the original problem was permissions etc. you could still have the issue as soon as you try to write the new text file.
Aug 20 '09 #25

P: 54
Thanks for the input. Opening notepad is still my #1 option. I hope i get it resolved.
Aug 20 '09 #26

Post your reply

Sign in to post your reply or Sign up for a free account.