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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating a file with C# 1

Status
Not open for further replies.

Henk1

Programmer
Oct 26, 2001
46
ZA
Good day,

I am creating a file with this statement...

xlApp.ActiveWorkbook.SaveAs(FilePath + "\\Summary.html",
Excel.XlFileFormat.xlHtml, Type.Missing,
Type.Missing, Type.Missing, Type.Missing,
Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing);

But I am getting this error:
"Cannot access read-only document 'Summary.html'."

How can I fix this?

Thanks for any help.
 
Sorry, maybe I should just point out that I am trying to create this file, and overwrite it if it exists.

I want to redirect to this page once it is saved. Because of this, the page is saved in my inetpub\ dir on the HDD.

Any ideas?
 
Use the Path class to first build your path (it handles directory separators for you):
Code:
string filePath = Path.Combine(FilePath + "Summary.html");
Then use the File class to see if the file exists, and if it's readonly:
Code:
bool fileExists;
bool fileReadOnly;

fileExists = File.Exists(filePath);
if (fileExists)
{
   FileAttributes fileAttr = File.GetAttributes(filePath);
   fileReadOnly = (fileAttr & FileAttributes.ReadOnly) == FileAttributes.ReadOnly;
}
Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Thanks alot ChipH! Really good stuff. Just a "fairly" stupid question...

After what you typed in there, after this

"fileExists = File.Exists(filePath);
if (fileExists)
{
FileAttributes fileAttr = File.GetAttributes(filePath);
fileReadOnly = (fileAttr & FileAttributes.ReadOnly) == FileAttributes.ReadOnly;
}"

I would save the file with this statement.
"xlApp.ActiveWorkbook.SaveAs(FilePath, Excel.XlFileFormat.xlHtml, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);"

But this actually skipped the whole if statement, since the file did not exist.

I still get this error message:
"'C:\inetpub\ cannot be accessed. The file may be read-only, or you may be trying to access a read-only location. Or, the server the document is stored on may not be responding."

Even though the file does not exist.
 
Well, the problem is that there is no such file. I searched my whole HDD for it, and I cannot find it. It has not been saved yet, as it crashes with this error message before it can get saved.

On a sidenote. How do you get those nifty "Quote" and "Code" blocks?
 
It's documented in the "Process TGML" popup at the bottom of the reply text area (right above the Submit Post button).

The one that's not documented (or wasn't the last time I checked) is the Link tag. It's format is:
[ignore][link [/ignore]{URL}[ignore]][/ignore]{descriptive text}[ignore][/url][/ignore].

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Code:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.Reflection; 

public class SaveXml : System.Web.UI.Page
{
		protected System.Web.UI.WebControls.Button btnSave;

	override protected void OnInit(EventArgs e)
	{
		//
		// CODEGEN: This call is required by the ASP.NET Web Form Designer.
		//
		InitializeComponent();
		base.OnInit(e);
	}
	private void Page_Load(object sender, System.EventArgs e)
	{
		// Put user code to initialize the page here
	}
	private void InitializeComponent()
	{    
		this.btnLoad.Click += new System.EventHandler(this.btnSave_Click);
		this.Load += new System.EventHandler(this.Page_Load);

	}
	private void btnSave_Click(object sender, System.EventArgs e)
	{
		try
		{
			//XmlTextReader reader = new XmlTextReader(Server.MapPath("myfile.html"));
			xlApp.ActiveWorkbook.SaveAs(Server.MapPath(Summary.html)",
   		 	Excel.XlFileFormat.xlHtml, Type.Missing,
   			Type.Missing, Type.Missing, Type.Missing,
   			Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing,
    			Type.Missing, Type.Missing, Type.Missing, Type.Missing); 
                }
                //...
	}
}
The Server.MapPath() will produce "c:\inetpub\ .
The Server.MapPath("/") returns "c:\inetpub\ and you can build your own path.
-obislavu-
 
thanks for all the help. However, I am still getting the same problem.

I just don't know what I am doing here. I copied your (obislavu) code for saving and I am still getting the same problem. It seems that the only diffirence is in the way the path is created/set up. The rest of my code looks exactly the same. It is pointing to the correct path in the error message, so it does not seem to be the path.

Thanks Chip for the help with the code and quote blocks.

Any other thoughts to wat it might be?
 
Ok, what I tried now, is to create and empty file, called summary.html in the correct directory, and then ran the program again.

Now the message it gets is:
"Cannot access read-only document 'Summary.html'."

And I checked, it is NOT read-only. Why would it do this?
 
Could this be a permissions issue?

Does the account that is accessing the code and trying to create/save the file have write permissions in the directory that it is trying to save in?

Randall2nd
 
I would presume so. ASPNET has admin rights to my machine. Do I use the ASPNET account to run the code? I think so. How do I check?

Well, if it is the ASPNET account, then I don't think it is permissions based.

I am really giving up with this now. I just don't know what to do...

Could it perhaps be with the following part of the save statement?

xlApp.ActiveWorkbook.SaveAs(Server.MapPath("Summary.html"), Excel.XlFileFormat.xlHtml, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

Intellisence describes this part as object ReadOnlyRecommended

Could this be it? If so, what do I type in there?
 
What about the properties of the virtual folder in IIS? This is distinct from properties that an NT user has...
Run the IIS admin tool and check the write properties for the virtual folder (root and each subsequent) for the file.

Turn your headache into my project!
Jeffrey R. Roberts
Insight Data Consulting
Access and SQL Server Development
 
ok, the persmissions that is set on my web site virtual directory is as follows.

I have anonymous access enabled, with a user called "IUSR_AT007359" with access to the guests group. Also, I am allowing IIS to control the password.

Then I also have digest authentication for windows domain servers and integrated windows authentication enabled.

Basic authentication is disabled.

Is this what I am looking for?

There are no sub-virtual folders in my web site virtual folder, except the standard folders.

Are these security settings correct? As far as I can figure out, there are no priviledges defined for the guests group in the windows shares, which includes which I presume gets used by users to log onto the web site.

Should I add the guests group to the share?

Also, I am not handeling any logon onto the website indipendantly in my code. So I presume (although I am not sure) that the anonymous logon is used? Is this the correct assumption?

Sorry, I do not know alot about security, and stopped fiddeling around with it, since it only screwed up working things, and made me reformat my machine to get rid of errors that came from fiddeling. :(
 
The Guests group typically has no permission to write to files. Try it yourself - create a new local user that is a member of the guests group, then login as that user. The things you can do will be severely limited.

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Well, that was the problem.

I want to just thank everyone that was involved in this, as the problem is now resolved. It seems that the anonymous logon just did not have the access to write a file.

Now, obviously I cannot give this user ID (anonymous login) admin access. So what kind of access must I give it?

Just normal read and write access in the share? Would this do?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top