当前位置:编程学习 > C#/ASP.NET >>

C#创建WORD对象问题,说我权限不够。

代码:

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.IO;


namespace licians
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button2;
protected System.Web.UI.WebControls.Button Button1;

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{    
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Button2.Click += new System.EventHandler(this.Button2_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void Button1_Click(object sender, System.EventArgs e)
{
object oMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();


oWord.Visible = true;
object fileName = @"D:\licians\test.dot";
oDoc = oWord.Documents.Open(ref fileName,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
}

private void Button2_Click(object sender, System.EventArgs e)
{
StreamReader objReader = new StreamReader("c:\\test.txt");
string sLine="";
ArrayList arrText = new ArrayList();

while (sLine != null)
{
sLine = objReader.ReadLine();
if (sLine != null)
arrText.Add(sLine);
}
objReader.Close();

foreach (string sOutput in arrText)
Response.Write(sOutput);

}




}
}






出问题的部分:

			object oMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();

oWord = new Word.Application(); 这个地方报错。


报错内容:
拒绝访问。 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 

异常详细信息: System.UnauthorizedAccessException: 拒绝访问。 

ASP.NET 未被授权访问所请求的资源。请考虑授予 ASP.NET 请求标识访问此资源的权限。ASP.NET 有一个在应用程序没有模拟时使用的基进程标识(通常,在 IIS 5 上为 {MACHINE}\ASPNET,在 IIS 6 上为网络服务)。如果应用程序正在通过 <identity impersonate="true"/> 模拟,则标识将为匿名用户(通常为 IUSR_MACHINENAME)或经过身份验证的请求用户。 

若要授予 ASP.NET 对文件的写访问权,请在资源管理器中右击该文件,选择“属性”,然后选择“安全”选项卡。单击“添加”添加适当的用户或组。突出显示 ASP.NET 帐户,选中所需访问权限对应的框。

源错误: 


行 55:  Word._Application oWord;
行 56:  Word._Document oDoc;
行 57:  oWord = new Word.Application();
行 58: 
行 59:

 
 

 



--------------------编程问答-------------------- 我的系统原来是FAT的,后来用convert命令改成NTFS了,但是传说中的“安全”选项卡还是没有出现。

曾经尝试过的方式:
1、把ASPNET用户和IUSER用户都加到Administrators :没效果
2、运行 Dcomcnfg --组建服务 --计算机--我的电脑--DCOM配置--Microsoft Word 文档--右键属性:把“安全”里边的几个能够给ASPNET授权的和IUSER授权的,都加上了。并且把“表示”里边选了“交互式用户”    还是不成。
3、把目标文件的文件夹,改成共享,也不成。

所以,我是彻底崩溃了,请大家帮个忙。谢谢了。


另外:测试了一个程序,Button2_Click事件里边的内容:
去读一个文件并且显示出来,是没问题的,看来不是文件的问题,应该是C#访问OFFICE的类库的问题。 --------------------编程问答--------------------  ASP.NET   对文件的写访问权,请在资源管理器中右击该文件,选择“属性”,然后选择“安全”选项卡。单击“添加”添加适当的
word所在的文件夹把权限改为可读写试试?everyone --------------------编程问答-------------------- yeefa 
 
等 级:
 发表于:2008-01-27 01:04:052楼 得分:0 
  ASP.NET       对文件的写访问权,请在资源管理器中右击该文件,选择“属性”,然后选择“安全”选项卡。单击“添加”添加适当的 
word所在的文件夹把权限改为可读写试试?everyone 
 ===================================================

这个也试验过了,不成。 --------------------编程问答-------------------- 顶一下,希望周末有人来看看^_^ --------------------编程问答-------------------- 问题没有解决,继续顶。 --------------------编程问答-------------------- 顶 --------------------编程问答-------------------- 我的系统原来是FAT的,后来用convert命令改成NTFS了,但是传说中的“安全”选项卡还是没有出现。 
=========
你在工具>>>文件夹选项>>>把 使用简单文件共享 前面的勾 去掉,就可以找到 “安全”了。

你给它everyone 写的权限再试试。还有也要考虑是不是磁盘格式问题。【你不是说已经转换成ntfs了,那你把程序移到另外台电脑上试试,看是不是磁盘格式问题。】 --------------------编程问答--------------------
开始-〉控制面板-〉管理工具-〉组件服务-〉 你的计算机-〉word组件-〉启动访问权限赋予everyone
--------------------编程问答-------------------- 依然无解>_< --------------------编程问答-------------------- 我也正被在问题难住了,不知道问题出在哪里?没招了。 --------------------编程问答-------------------- 我的出现的错误:System.Runtime.InteropServices.COMException: 命令失败

            object objFileName = FileName;
            object objReadOnly = false;
            object objVisible = false;
            object objNull = Missing.Value;

            doc = wordApp.appClass.Documents.Open(ref objFileName,
                ref objNull, ref objNull, ref objReadOnly, ref objNull, ref objNull, ref objNull, ref objNull,
                ref objNull, ref objNull, ref objNull, ref objVisible, ref objNull, ref objNull, ref objNull, ref objNull);
--------------------------------------------
wordApp.appClass.Documents.Open 这行到这句报错 --------------------编程问答-------------------- http://www.msdners.com/dev-archive/183/12-46-1836841.shtm

不要装office sp3补丁
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,