Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Use NotePad to oppen a text file

Status
Not open for further replies.

markask

Programmer
Apr 28, 2005
167
0
0
GB
Hi,

I run my ASP page by IIS in my PC. In the ASP code, how to use NotePad to oppen a text file?

Thank you in advance.
 
Word is made for ActiveX/COM automation... I don't know if the same is true for Notepad.

When you create an instance of Word.Application you are creating an instance of a COM object named Application that is exposed in a library named Word.

I tend to doubt that Notepad exposes any such objects.
 
IUSR_MachineName is the account that IIS uses by default to determine which actions may be taken by an ASP page. It is a local account on the web server machine so replace the MachineName with the actual name of your web server computer.

This is a local account (non-domain) and it usually have very very limited access rights to the server for security reasons.

You might check out the security in the IIS Admin tool, if it is still at its default setting then this is the account being used. Try granting permissions to this account for Notepad.exe and also into the folder in which you create the text file... or alternatively you could change the default account to something with more access rights... local admin would be fine for a short test but obviously you don't want to leave it that way because its a big security risk if the server is open to the public.

 
markask, try this code.

<SCRIPT LANGUAGE="VBScript">
Set objShell = CreateObject("Wscript.Shell")
myfile = "notepad.exe " & chr(34) & "C:\my path\test.txt" & chr(34)
objShell.Run myfile
</SCRIPT>

I believe that sheco is correct in that the other code opened a com object for Word and that Notepad.exe would not have one.
This code though executes the shell and launches notepad.exe.
The big difference here is that this script acts locally to the PC, not the server. If you accessed your web site from a different PC the shell command would be acting locally to the PC you were accessing FROM and the files would be loading from that PC.
Since this is accessing the file system of the PC out of the context of the browser it fires off an ActiveX warning to let you know that the script on the web page is accessing local files. Of course if you have your warnings turned off you will not see that message.

According to what I have read, if you try and use paths with spaces in them you have to encase them with double quotes but that the script will choke on the double quotes and that is why the example above uses the CHR(34) instead.
I tested this without them however and it still worked normally for me. Perhaps this is because the code executes from within IE rather than the Windows environment. You are better off using the CHR(34) example as it should work even if you make it a local script in VB rather than through a browser.
 
theniteowl,

I have just stested your code in my WindowsXP, That didn't make difference...I got everything fine but the .txt file didn't displayed.
Strange...
 
OK, when I tested here I had just run a .htm file from my desktop and it worked fine as posted.
When I tried it on a remote server it had issues.

I tested and came up with this.

<SCRIPT LANGUAGE="VBScript">
Set objShell = CreateObject("Wscript.Shell")
myfile = "notepad.exe " & chr(34) & "\\servername\inetpub\scripts\myfile.txt" & chr(34)
objShell.Run myfile
</SCRIPT>

The only difference is that you have to use a full path not a relative one. The file could be on any server, not just the web server but you have to give the full path of it and you must have rights to the folder/file in question.

I stated earlier but did not consider the implication that since this code runs clientside, the relative path on the server no longer applied and a full path would be needed just as if you were browsing to the file from your desktop.
Silly of me really since I pointed out the difference. :)
 
After a bit of research I found out that if you use ASP code on the page to try and launch a server-side instance of Notepad.exe, you are actually opening that instance under a different account. You will not see the window open but under task manager you can see that more and more notepad.exe instances are running and you may not be able to shut them down. I tried restarting IIS services and they still would not go away and were eating up resources.

It looks like the best way is using the VBScript code which executes client-side. The drawback is getting the activeX warning and having to use full paths rather than relational paths on the server. You will also have to make sure the login ID of the person executing the script has sufficient rights to the folder/file being linked but since this is supposed to just be yourself it should not be a problem.

 
theniteowl, what happens if you tweak the security settings in IIS so that anonymous access is not allowed... and then you open the page using the browser on the server machine itself?
 
I have not tried anything with the anonymous access but anytime you execute a script in the server context I would suspect that it would use the IWAM_Servername account to execute for that connection. I do not think it would ever distinguish between a local browser instance and a remote one when running through IIS.
It's all theory on my part though. All I know for sure is that testing these scripts hung up my web server as I maxed out the connections and they would not release. I had to restart my PC. :(
All the script instances that executed serverside DID start the application but not under the local account so it was not visible or accessible to even shut it down from Task manager. Perhaps if I had full admin to my own PC I could have ended the tasks. I have pretty close to full admin now though and could not.
 
If you turn off anonymous access then the process will execute under the account of the browser client. I was wondering if you were actually logged into server locally if that would mean it executes in a way that you can see it.
 
I am logged in under my own ID. I am running IIS on my PC rather than on a server.
I generally do my development on servers but do not have access to many things there so I do some testing on my own device.

Would you have to log into the server under the IWAM or IUSR account?
 
I mean in the IIS Admin tool ... the MMC SnapIn thing... in there if you turn off anonymous access then instead of using IUSR_MachineName, IIS will use your current login as the security context for ASP pages... I was wondering if the ASP was running in the security context of the current console user if the Notepad would be visible.

I'm having a hard time explaining this clearly!
 
I shut off anonymous access and it made no difference. I now have an instance of notepad.exe running under task manager that I cannot close out of.

Possibly something with server.createobject might work.
The only samples I have seen though cause the server to either open a com object or to open a shell, then the script has the ability to pull back data returned from that shell and use it within the script. At no point does the shell window appear visibly at the console though. I think it is just an application thread that has to be manipulated programmatically.
 
Hi all,

Can we use API in ASP something like in VB?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top