6

OK to add clarification after the comments posted and the fact I realise my original question was massively confusing. This is what I am trying to achieve....

This will be an web application running on a local intranet and not over the internet. Ultimately I want to be open a network folder location from within the the web application. So for example the web application creates folders on the file server with a set structure i.e:

\server\jobnumber\exhibitreference\image1 \server\jobnumber\exhibitreference\image2

I want the user to be able to navigate to the record and click a link to open it's matching folder location. The users, web server and file server are all on the same domain.

The code below was just used as an example to try and get it working for a file/folder on my local machine before I moved off to trying a remote folder. I appreciate this was confusing.

Original question

I have created .Net/C# web application and I want to open a text file at a specified location. The code below is working fine when run on IIS Express but once published to IIS it does not work.

At present IIS Express and IIS 7 are running on my local machine. The IIS application pool is configured to run under my domain account (had to do this as we have a double hop issue of authentication to SQL server) So far I have the following code:

ProcessStartInfo processStartInfo = new ProcessStartInfo();
processStartInfo.WorkingDirectory = @"C:\Users\pcustance\Desktop\";
processStartInfo.FileName = @"notepad.exe";
processStartInfo.Arguments = "test.txt";
processStartInfo.WindowStyle = ProcessWindowStyle.Maximized;
processStartInfo.CreateNoWindow = true;

Process process = Process.Start(processStartInfo);

Watching the system processes through task manager I can see that the process "notepad.exe" gets created successfully but no window opens. It says the process is running under "pcustance" account but I can only see it when I select "show processes from all users" in task manager.

Is the window not launching because somehow it is being run under the wrong account?

I have also tried:

Process.Start("C:\Users\pcustance\Desktop\test.txt");

As before, this works in IIS Express but not on IIS7.

Any help is greatly appreciated.

Solution

At the moment I have had to resort to using Internet Explorer which supports the use of local links out the box. The browser can be pointed at a network location with the following:

file:///\\server\folder\location
or 
file://///server/folder/location
oceanexplorer
  • 1,209
  • 3
  • 11
  • 24
  • http://stackoverflow.com/questions/10174156/open-file-with-associated-application can you check this? – Aurora Dec 03 '13 at 15:25
  • If i can understand, you want a asp page to open notepad to client's pc? – kostas ch. Dec 03 '13 at 15:27
  • Should you not shell this from client side, using Javascript or the like? It seems clear that the IIS account it runs under does not function as a normal logged on user, and thus, won;t know where to "open" / draw notepad (Desktop / display wise). I would open it from client side. But that's just me. – Louis van Tonder Dec 03 '13 at 15:29
  • @LouisvanTonder that's why i was asking. You r right.You can NOT use client's notepad to do that.oceanexplorer must study more about web. – kostas ch. Dec 03 '13 at 15:31
  • @kostasch. Mine is at 2 mins, yours at 3mins... mhuahaha but yeah. Think we are on to the same thing. – Louis van Tonder Dec 03 '13 at 15:32
  • Maybe by trying node.js and that's just a maybe. – kostas ch. Dec 03 '13 at 15:32
  • @LouisvanTonder hehehhe. You can not even read clients directory through web app because of security. – kostas ch. Dec 03 '13 at 15:33
  • 4
    why are you trying to do this with II7? II7 isn't designed to do what you are attempting to do (it's for hosting websites). If it's supposed to run on a server (your debugging locally I assume), a windows service might be a better way to go. I think the question: "why do you want to do this" might lead to better answers. – Alexander Matusiak Dec 03 '13 at 15:34
  • Ok to clarify a few points. This will be a web application running on a local network only (not over the internet). The plan is for it to open folders at a network location on the internal network. At present there are permission issues with that, so I thought I would keep it simple in just opening a file on the local machine. IIS Express runs this code fine. – oceanexplorer Dec 03 '13 at 15:47
  • Are you sure the user that runs the application pool has access to do what you want it to do on the file system ? ie: NTFS permissions. – Francis Ducharme Dec 03 '13 at 15:59
  • @AlexanderMatusiak I have created a web application that will be run on an internal network. I would like to open a network folder location from the application. I just started off with a local file to try and get it working. Perhaps it just isn't possible. – oceanexplorer Dec 03 '13 at 16:01
  • You seem to be asking a few things: 1) Can I run a console window on the server, from an ASP.Net application. You simply should not do this. When you want to see the file in question, open the file the normal user way; write a separate console app for viewing it if you prefer. 2) How do I get my production app to access this file? That's all about Windows permissions; assign a user to the AppPool the site runs on, assign that user permissions to access the dir and file in question, and you've either solved that or are at least closer. 3) Or are neither of these your actual question? – Chris Moschini Dec 03 '13 at 16:05
  • @ChrisMoschini Thanks for the reply. I think my original question was massively confusing, I apologise. I'm writing a web based application that will only be used on an internal network. The application creates a folder structure on our file server which matches the structure of our records in the database e.g. \\server\jobnumber\exhibitreference\image1 when the user navigates to their chosen record I want to be able to open the matching folder location. – oceanexplorer Dec 03 '13 at 16:48

3 Answers3

3

All your code runs within asp.net which is hosted in a server (via IIS).

The code you have written will execute in the context of where your asp.net app is hosted.

While doing web development using visual studio, the "server" and the "client" (i.e. the browser) is usually the same computer. The code executes in the context of a localized development server. Your browser will make requests to "that" server. Therefore the code you wrote is bound to give you the illusion that you've started the process - notepad.exe

The stuff you've actually implemented about doesn't apply for web applications in general. It isn't even feasible. Since the "server" and "client" are two different machines now. The closest you can get into implementing this requirement is serving up the file as response. To the end user, this is equivalent to downloading (in most cases).

Edit:

Your options are serving up the file as-is shown in the code:

Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", _
  "attachment; filename=""" & filename & """");

This will force user to download the file on the client with a default name as specified by value in filename. Actually you can vary the Content-Disposition part to instruct the browser how to load. However, it depends on the target browser. Here is a small example:

FileStream MyFileStream = new FileStream(@"d:\inetpub\wwwroot\small.txt", FileMode.Open);
long FileSize;
FileSize = MyFileStream.Length;
byte[] Buffer = new byte[(int)FileSize];
MyFileStream.Read(Buffer, 0, (int)MyFileStream.Length);
MyFileStream.Close();
Response.ContentType="text/plain";
Response.AddHeader( "content-disposition","inline; filename=sample.txt");
Response.BinaryWrite(Buffer);

You should research a few articles and experiment. Here are some links to start with:

  1. Content-Disposition:What are the differences between "inline" and "attachment"?
  2. http://www.nullskull.com/articles/20011006.asp
  3. http://www.windowsdevcenter.com/pub/a/dotnet/2002/04/01/asp.html
Community
  • 1
  • 1
deostroll
  • 11,661
  • 21
  • 90
  • 161
  • I thought as much, and really didn't expect this to work at all but seemingly it did when I tested it in IIS Express but understand that this was because I was running the web server all on the same machine. I just wonder what my options are now for opening an remote folder location in an intranet environment. – oceanexplorer Dec 03 '13 at 16:11
0

The intent of your question is still not clear.

Are you...

A. trying to connect to a web application (hosted with IIS) from a client browser and access a file from a network share (relative to the server) for the client to consume?

B. trying to connect to a web application (hosted with IIS) from a client browser and have the application access a file from the network to be used by the server (like a .txt file containing information that the application needs for some processing)?

C. trying to connect to a web application (hosted with IIS) from a client browser and have the hosted application access a file from a network share (relative to the client) for the server to consume?

My assumption is you are attempting (B) and if so, you should use System.IO and access your files programmatically instead of trying to launch a process.

UPDATED BELOW

If you are trying to connect to a web application, and launch a local process (such as notepad.exe) on the client you cannot do so by launching a process. Otherwise MyVirus.com could launch local processes on my machine and that would be a nightmare. The web application is only going to launch processes on the server, never the client.

The best you can do is open the file (from the server) and send a Response back to the client with the contents of the file or send the path and file name to the client and open it via javascript or an HTML5 FileReader.

ADDITIONAL UPDATE

You should be able to open an image (or other content consumable in a browser) from a UNC path as long as you are your Application Pool Identity has permissions and also, are you using impersonation (identity impersonate="true" userName="someaccount" password="somepassword")?

ONE LAST UPDATE

Unfortunately even opening a folder location requires a local process to launch (explorer.exe). That's simply not going to happen with modern browsers for security reasons. You could cook up something wonky like a local windows service on each client machine that checks a database and if it finds x it launches the local explorer.exe to the path stored in the database... but it would have to be checking every second and that sounds like a really bad idea.

Other than that maybe something like this File Explorer Control from Telerik or this File View Control would serve your purposes. (Disclaimer, I don't know anything about either of these controls just thought they might help).

user2315985
  • 2,848
  • 5
  • 17
  • 18
  • Thanks for your response: I would go with C as my answer. My web application creates a folder structure on our file server, this structure is tied to information from the database. I want a user to be able to access a record in the web application and open the correct folder location on the file server. The client browser, web server and file server all run on the same network. – oceanexplorer Dec 03 '13 at 16:20
  • So from the client, you visit a website, and you are presented with a file path and you want the client to open a file (or launch notepad.exe) and access a file from that patch such as \\myserver\mypath\myfile.txt? – user2315985 Dec 03 '13 at 16:30
  • Pretty much, I have edited my original question to state that I want the user to be able to open a network folder. The users/web server/file server are all on the same domain. – oceanexplorer Dec 03 '13 at 16:44
  • You're pretty close. I understand what you have said and I presume that sending the response back to the client via javascript would trigger a save/open dialog. My issue really is that I don't want to open a file, I just want to open the folder location. Please see my further edited original question for a better explanation. – oceanexplorer Dec 03 '13 at 16:56
  • Added "One Last Update" to my answer above. – user2315985 Dec 03 '13 at 18:10
-2

please see this page

http://msdn.microsoft.com/en-us/library/58wxa9w5(v=vs.100).aspx

Most importantly this line

Use when you are working with an existing project or your site targets an older version of IIS, such as IIS 6, and it is not very important that your testing environment match the production environment closely. This server option is the default in Visual Studio. However, the Visual Studio Development Server runs in a different security context than full IIS, and may fail to reveal errors that can occur when you deploy to a production version of IIS.

The issue is that IISExpress and the local dev server run under your security context. This allows them much more freedom to start processes and have access to files on the local system.

IIS however runs in a much stricter security context and has a limited access to the machine at hand. Imagine if IIS could do what you are proposing above. you could basically run any arbitrary code on the webserver.

Fran
  • 6,440
  • 1
  • 23
  • 35