Consolesw.writelinee("{0}! = {1}"

其他回答(1)
学习学习!!!
园豆:1613
&&&您需要以后才能回答,未注册用户请先。using_百度百科
关闭特色百科用户权威合作手机百科
收藏 查看&using本词条缺少概述、信息栏、名片图,补充相关内容使词条更完整,还能快速升级,赶紧来吧!
定义一个范围,将在此范围之外释放一个或多个对象。using (Font font1 = new Font(&Arial&, 10.0f))
}using 有两个主要用途:用于定义一个范围,在此范围的末尾将释放对象
(CLR) 自动释放用于存储不再需要的对象的内存。内存的释放具有不确定性;一旦 CLR 决定执行,就会释放内存。但是,通常最好尽快释放诸如和网络连接这样的有限资源。
using 语句允许程序员指定使用资源的对象应当何时释放资源。为 using 语句提供的对象必须实现 IDisposable 接口。此接口提供了 Dispose 方法,该方法将释放此对象的资源。
可以在到达 using 语句的末尾时,或者在该语句结束之前引发了异常并且控制权离开语句块时,退出 using 语句。
1. 可以在 using 语句中声明对象(如上所示),或者在 using 语句之前声明对象,如下所示:
Fontfont2=newFont(&Arial&,10.0f);
using(font2)
//usefont2
//2.可以有多个对象与using语句一起使用,但是必须在using语句内部声明这些对象,如下所示:
using(Fontfont3=newFont(&Arial&,10.0f),font4=newFont(&Arial&,10.0f))
//Usefont3andfont4.
下面的示例显示用户定义类可以如何实现它自己的 Dispose 行为。注意类型必须从 IDisposable 继承。
classC:IDisposable
publicvoidUseLimitedResource()
Console.WriteLine(&Usinglimitedresource...&);
voidIDisposable.Dispose()
Console.WriteLine(&Disposinglimitedresource.&);
classProgram
staticvoidMain()
using(Cc=newC())
c.UseLimitedResource();
Console.WriteLine(&Nowoutsideusingstatement.&);
Console.ReadLine();
用于为创建别名或导入其他命名空间中定义的类型
①.允许在中使用类型,这样,您就不必在该命名空间中限定某个类型的使用:
using System.T
②.为或类型创建别名。
using Project = PC.MyCompany.P
using 指令的范围限制为包含它的文件。
创建 using 别名,以便更易于将限定到或类型。
创建 using 指令,以便在中使用类型而不必指定命名空间。using 指令不为您提供对嵌套在指定中的任何命名空间的访问。
分为两类:用户定义的命名空间和的。用户定义的是在代码中定义的命名空间。若要查看的命名空间的列表1.下面的示例显示了如何为定义和使用 using 别名:
namespacePC
//Defineanaliasforthenestednamespace.
usingProject=PC.MyCompany.P
//Usethealias
Project.MyClassmc=newProject.MyClass();
namespaceMyCompany
namespaceProject
publicclassMyClass{}
2.下面的示例显示了如何为类定义using指令和using别名:
//cs_using_directive2.cs
//Usingdirective.
//Usingaliasforaclass.
usingAliasToMyClass=NameSpace1.MyC
namespaceNameSpace1
publicclassMyClass
publicoverridestringToString()
return&YouareinNameSpace1.MyClass&;
namespaceNameSpace2
classMyClass
namespaceNameSpace3
//Usingdirective:
usingNameSpace1;
//Usingdirective:
usingNameSpace2;
classMainClass
staticvoidMain()
AliasToMyClasssomevar=newAliasToMyClass();
Console.WriteLine(somevar);
You are in NameSpace1.MyClass
=========================================================================================在.NET大家庭中,有不少的承担了多种角色,例如new关键字就身兼数职,除了能够创建对象,在继承体系中隐藏成员,还在声明中约束可能用作类型参数的参数,在详细讨论using的多重身份的基础上来了解.NET在语言机制上的简便与深邃。
那么,using的多重身份都体现在哪些方面呢,我们先一睹为快吧:
· 创建别名
· 强制资源清理
下面,本文将从这几个角度来阐述using的多彩应用。using作为引入指令的用法规则为:
在.NET程序中,最常见的代码莫过于在的开头引入System,其原因在于System命名空间中封装了很多最基本最常用的操作,下面的代码对我们来说最为熟悉不过:
这样,我们在程序中就可以直接使用中的类型,而不必指定详细的类型名称。using指令可以访问嵌套。
是.NET程序在逻辑上的组织结构,而并非实际的,是一种避免类名冲突的方法,用于将不同的组合划分的方式。例如,在.NET中很多的基本类型都位于System,数据操作类型位于System.Data命名空间,· using类似于的import指令,都是引入(Java中称作包)这种逻辑结构;而不同于C语言中的#include指令,用于引入实际的类库,
· using引入,并不等于编译时加载该命名空间所在的,程序集的加载决定于程序中对该程序集是否存在调用操作,如果代码中不存在任何调用操作则编译器将不会加载using引入命名空间所在程序集。因此,在源文件开头,引入多个,并非加载多个,不会造成“过度引用”的弊端。 using为创建别名的用法规则为:
using alias = namespace |
其中namespace表示创建的别名;而type表示创建类型别名。例如,在.NET Office应用中,常常会引入Microsoft.Office.Interop.Word.dll,在引入时为了避免繁琐的类型输入,我们通常为其创建别名如下:
using MSWord = Microsoft.Office.Interop.W
这样,就可以在程序中以MSWord来代替Microsoft.Office.Interop.Word前缀,如果要创建Application对象,则可以是这样,
private static MSWord.Application ooo = new MSWord.Application();
同样,也可以创建类型的别名,用法为:
而创建别名的另一个重要的原因在于同一cs文件中引入的不同中包括了相同名称的类型,为了避免出现名称冲突可以通过设定别名来解决,例如:  namespace Boyspace
public class Player
public static void Play()
System.Console.WriteLine(&Boys play football.&);
namespace Girlspace
public class Player
public static void Play()
System.Console.WriteLine(&Girls play violin.&);
以using创建别名,有效的解决了这种可能的命名冲突,尽管我们可以通过类型全名称来加以区分,但是这显然不是最佳的解决方案,using使得这一问题迎刃而解,不费丝毫功夫,同时在编码规范上看来也更加的符合编码要求。4.1 由来
要理解清楚使用using语句强制清理资源,就首先从了解Dispose模式说起,而要了解Dispose模式,则应首先了解.NET的机制。这些显然不是本文所能完成的宏论,我们只需要首先明确的是.NET提供了Dispose模式来实现显式释放和关闭对象的能力。
Dispose模式
Dispose模式是.NET提供的一种显式清理对象资源的约定方式,用于在.NET 中释放对象封装的非托管资源。因为非托管资源不受GC控制,对象必须调用自己的Dispose()方法来释放,这就是所谓的Dispose模式。从概念角度来看,Dispose模式就是一种强制资源清理所要遵守的约定;从实现角度来看,Dispose模式就是让要一个类型实现IDisposable接口,从而使得该类型提供一个公有的Dispose方法。
本文不再讨论如何让一个类型实现Dispose模式来提供显示清理非托管资源的方式,而将注意集中在如何以using语句来简便的应用这种实现了Dispose模式的类型的资源清理方式。我们在与章节将有详细的讨论。
using语句提供了强制清理对象资源的便捷操作方式,允许指定何时释放对象的资源,其典型应用为:
using (Font f = new Font(&Verdana&, 12, FontStyle.Regular))
//执行文本绘制操作
Graphics g = e.G
Rectangle rect = new Rectangle(10, 10, 200, 200);
g.DrawString(&Try finally dispose font.&, f, Brushes.Black, rect);
}//运行结束,释放f对象资源
在上述典型应用中,using语句在结束时会自动调用欲被清除对象的Dispose()方法。因此,该Font对象必须实现IDispose接口,才能使用using语句强制对象清理资源。我们查看其类型定义可知:
public sealed class Font : MarshalByRefObject, ICloneable, ISerializable, IDisposable
Font类型的确实现了IDisposeable接口,也就具有了显示回收资源的能力。然而,我们并未从上述代码中,看出任何使用Dispose方法的蛛丝马迹,这正式using语句带来的简便之处,其实质究竟怎样呢?
要想了解using语句的执行本质,了解在背后做了哪些手脚,就必须回归到IL代码中来揭密才行:
.method public hidebysig static void Main() cil managed
.entrypoint
// 代码大小 40 (0x28)
.maxstack 4
.locals init ([0] class [System.Drawing]System.Drawing.Font f,
[1] bool CS$4$0000)
IL_0000: nop
IL_0001: ldstr &Verdana&
IL_0006: ldc.r4 12.
IL_000b: ldc.i4.0
IL_000c: newobj instance void [System.Drawing]System.Drawing.Font::.ctor(string,float32,
valuetype [System.Drawing]System.Drawing.FontStyle)
IL_0011: stloc.0
……部分省略……
} // end .try
……部分省略……
IL_001f: callvirt instance void [mscorlib]System.IDisposable::Dispose()
IL_0024: nop
IL_0025: endfinally
} // end handler
IL_0026: nop
IL_0027: ret
} // end of method UsingDispose::Main
显然,在自动将using生成为try-finally语句,并在finally块中调用对象的Dispose方法,来清理资源。
在.NET规范中,建议开放人员在调用一个类型的Dispose()或者Close()方法时,将其放在的finally块中。根据上面的分析我们可知,using语句正是隐式的调用了类型的Dispose方法,因此以下的代码和上面的示例是完全等效的:
Font f2 = new Font(&Arial&, 10, FontStyle.Bold);
//执行文本绘制操作
Graphics g = new Graphics();
Rectangle rect = new Rectangle(10, 10, 200, 200);
g.DrawString(&Try finally dispose font.&, f2, Brushes.Black, rect);
if (f2 != null)
((IDisposable)f2).Dispose();
· using只能用于实现了IDisposable接口的类型,禁止为不支持IDisposable接口的类型使用using语句,否则会出现编译时错误;
· using语句适用于清理单个非托管资源的情况,而多个非托管对象的清理最好以try-finnaly来实现,因为嵌套的using语句可能存在隐藏的Bug。内层using块引发异常时,将不能释放外层using块的对象资源。
· using语句支持初始化多个,但前提是这些变量的类型必须相同,例如:
using(Pen p1 = new Pen(Brushes.Black), p2 = new Pen(Brushes.Blue))
否则,编译将不可通过。不过,还是有变通的办法来解决这一问题,原因就是应用using语句的类型必然实现了IDisposable接口,那么就可以以下面的方式来完成初始化操作,
using (IDisposable font = new Font(&Verdana&, 12, FontStyle.Regular), pen = new Pen(Brushes.Black))
float size = (font as Font).S
Brush brush = (pen as Pen).B
另一种办法就是以使用try-finally来完成,不管初始化的对象类型是否一致。
· Dispose方法用于清理对象封装的非托管资源,而不是释放对象的内存,对象的内存依然由器控制。
· 程序在达到using语句末尾时退出using块,而如果到达语句末尾之前引入异常则有可能提前退出。
· using中初始化的对象,可以在using语句之前声明,例如:
Font f3 = new Font(&Verdana&, 9, FontStyle.Regular);
using (f3)
//执行文本绘制操作
}一个简单的,多种不同的应用场合。本文从比较全面的角度,诠释了using在.NET中的多种用法,值得指出的是这种用法并非实现于.NET的所有高级语言,本文的情况主要局限在C#中。
新手上路我有疑问投诉建议参考资料 查看Microsoft Outlook 11.0 Object LibraryI am&retrieving&message&from&the&Inbox&as&follows:&(MS&Article&ID&310258:&How&to&use&the&Microsoft&Outlook&Object&Library&to&retrieve&a&message&from&the&Inbox&by&using&Visual&C#&.NET)&&br/&&br/&Try&&br/&Dim&oApp&As&Outlook.Application&=&New&Outlook.Application&&br/&Dim&oNS&As&Outlook.NameSpace&=&oApp.GetNamespace(&mapi&)&&br/&&&oNS.Logon(Missing.Value,&Missing.Value,&False,&True)&&br/&Dim&oInbox&As&Outlook.MAPIFolder&=&oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)&&br/&Dim&oItems&As&Outlook.Items&=&oInbox.Items&&br/&Dim&oMsg&As&Outlook.MailItem&=&CType(oItems.GetFirst,&Outlook.MailItem)&&br/&&&Console.WriteLine(oMsg.Subject)&&br/&&&Console.WriteLine(oMsg.SenderName)&&br/&&&Console.WriteLine(oMsg.ReceivedTime)&&br/&&&Console.WriteLine(oMsg.Body)&&br/&&br/&Dim&AttachCnt&As&Integer&=&oMsg.Attachments.Count&&br/&&br/&&&oMsg.Attachments.Item(1).SaveAsFile(&C:\Temp\Attach&)&&br/&&&Console.WriteLine(&Attachments:&&&+&AttachCnt.ToString)&&br/&&&If&AttachCnt&&&0&Then&&br/&Dim&i&As&Integer&=&1&&br/&&&&&While&i&&=&AttachCnt&&br/&&&&&&&Console.WriteLine(i.ToString&+&&-&&+&oMsg.Attachments(i).DisplayName)&&br/&&&&&&&System.Math.Min(System.Threading.Interlocked.Increment(i),&i&-&1)&&br/&&&&&End&While&&br/&&&End&If&&br/&&&oMsg.Display(True)&&br/&&&oNS.Logoff()&&br/&&&oMsg&=&Nothing&&br/&&&oItems&=&Nothing&&br/&&&oInbox&=&Nothing&&br/&&&oNS&=&Nothing&&br/&&&oApp&=&Nothing&&br/&Catch&e&As&Exception&&br/&&&Console.WriteLine(&{0}&Exception&caught:&&,&e)&&br/&End&Try&&br/&&br/&When&I&run&the&sample&I&get&a&message&box&saying&&A&program&is&trying&to&access&e-mail&addresses&you&have&stored&in&Outlook.&Do&you&want&to&allow&this?&If&this&unexpected,&it&may&b&a&virus&and&you&should&choose&&No&.&&How&can&I&turn&off&this&message&box?&&br/&&br/&Secondly,&how&can&I&save&the&attached&rather&than&opening&the&email?&&br/&&br/&Is&there&any&document&that&describes&methods&and&properties&exposed&by&Microsoft&Outlook&Object&Library?&I&could&not&find&any&link.&Thanks&(C) 2009 Microsoft Corporation. 版權所有,並保留一切權利。Thu, 11 Dec :36 Zc6-4d89-995e-1cc4https://social./Forums/zh-TW/c6-4d89-995e-1cc4/microsoft-outlook-110-object-library?forum=winforms#c6-4d89-995e-1cc4https://social./Forums/zh-TW/c6-4d89-995e-1cc4/microsoft-outlook-110-object-library?forum=winforms#c6-4d89-995e-1cc4MigrationUser 1https://social.:443/profile/migrationuser%201/?type=forumMicrosoft Outlook 11.0 Object LibraryI am&retrieving&message&from&the&Inbox&as&follows:&(MS&Article&ID&310258:&How&to&use&the&Microsoft&Outlook&Object&Library&to&retrieve&a&message&from&the&Inbox&by&using&Visual&C#&.NET)&&br/&&br/&Try&&br/&Dim&oApp&As&Outlook.Application&=&New&Outlook.Application&&br/&Dim&oNS&As&Outlook.NameSpace&=&oApp.GetNamespace(&mapi&)&&br/&&&oNS.Logon(Missing.Value,&Missing.Value,&False,&True)&&br/&Dim&oInbox&As&Outlook.MAPIFolder&=&oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)&&br/&Dim&oItems&As&Outlook.Items&=&oInbox.Items&&br/&Dim&oMsg&As&Outlook.MailItem&=&CType(oItems.GetFirst,&Outlook.MailItem)&&br/&&&Console.WriteLine(oMsg.Subject)&&br/&&&Console.WriteLine(oMsg.SenderName)&&br/&&&Console.WriteLine(oMsg.ReceivedTime)&&br/&&&Console.WriteLine(oMsg.Body)&&br/&&br/&Dim&AttachCnt&As&Integer&=&oMsg.Attachments.Count&&br/&&br/&&&oMsg.Attachments.Item(1).SaveAsFile(&C:\Temp\Attach&)&&br/&&&Console.WriteLine(&Attachments:&&&+&AttachCnt.ToString)&&br/&&&If&AttachCnt&&&0&Then&&br/&Dim&i&As&Integer&=&1&&br/&&&&&While&i&&=&AttachCnt&&br/&&&&&&&Console.WriteLine(i.ToString&+&&-&&+&oMsg.Attachments(i).DisplayName)&&br/&&&&&&&System.Math.Min(System.Threading.Interlocked.Increment(i),&i&-&1)&&br/&&&&&End&While&&br/&&&End&If&&br/&&&oMsg.Display(True)&&br/&&&oNS.Logoff()&&br/&&&oMsg&=&Nothing&&br/&&&oItems&=&Nothing&&br/&&&oInbox&=&Nothing&&br/&&&oNS&=&Nothing&&br/&&&oApp&=&Nothing&&br/&Catch&e&As&Exception&&br/&&&Console.WriteLine(&{0}&Exception&caught:&&,&e)&&br/&End&Try&&br/&&br/&When&I&run&the&sample&I&get&a&message&box&saying&&A&program&is&trying&to&access&e-mail&addresses&you&have&stored&in&Outlook.&Do&you&want&to&allow&this?&If&this&unexpected,&it&may&b&a&virus&and&you&should&choose&&No&.&&How&can&I&turn&off&this&message&box?&&br/&&br/&Secondly,&how&can&I&save&the&attached&rather&than&opening&the&email?&&br/&&br/&Is&there&any&document&that&describes&methods&and&properties&exposed&by&Microsoft&Outlook&Object&Library?&I&could&not&find&any&link.&Thanks&Sat, 22 Jan :26 ZT20:59:26Zhttps://social./Forums/zh-TW/c6-4d89-995e-1cc4/microsoft-outlook-110-object-library?forum=winforms#8e34c05a-91cb--5chttps://social./Forums/zh-TW/c6-4d89-995e-1cc4/microsoft-outlook-110-object-library?forum=winforms#8e34c05a-91cb--5cMigrationUser 1https://social.:443/profile/migrationuser%201/?type=forumMicrosoft Outlook 11.0 Object LibraryYou can't&get&around&that&warning...&the&user&has&to&agree&(or&revert&to&an&older&edition&of&Outlook&-&I&think&outlook&2000&w/o&any&SP&or&HF&doesn't&ask&this).&&If&this&is&to&be&a&fully&automated&app&(no&user),&you'll&need&a&POP3&control&rather&than&Outlook.&br/&&br/&You've&saved&the&first&attachment&with&oMsg.Attachments.Item(1).SaveAsFile.&&You&should&put&that&line&in&your&attachment&loop&and&use&.Item(i).&&Also&specify&file&names...&br/&&br/&Just&omit&the&oMsg.Display(True)&to&keep&from&opening&the&email.Mon, 24 Jan :02 ZT11:38:02Zhttps://social./Forums/zh-TW/c6-4d89-995e-1cc4/microsoft-outlook-110-object-library?forum=winforms#3c-40ef-b70c8b1https://social./Forums/zh-TW/c6-4d89-995e-1cc4/microsoft-outlook-110-object-library?forum=winforms#3c-40ef-b70c8b1MigrationUser 1https://social.:443/profile/migrationuser%201/?type=forumMicrosoft Outlook 11.0 Object LibraryHi rkimble&br/&&br/&It&came&up&with&an&error&saying&&Cannot&save&the&attachment.&You&don't&have&appropriate&permission&to&perform&this&operation.&&Following&are&the&details:&br/&&br/&&br/&System.Exception&(0xD4970005):&Cannot&save&the&attach&br/&ment.&You&don't&have&appropriate&permission&to&perform&this&operation.&br/&&&&at&Outlook.Attachment.SaveAsFile(String&Path)&br/&&&&at&RetrieveMessageFromInbox.Module1.Main()&in&C:\Documents&and&Settings\pbang&br/&a\My&Documents\Visual&Studio&Projects\RetrieveMessageFromInbox\Module1.vb:line&4&br/&7&Exception&caught:&br/&Press&any&key&to&continueMon, 24 Jan :57 ZT18:22:57Zhttps://social./Forums/zh-TW/c6-4d89-995e-1cc4/microsoft-outlook-110-object-library?forum=winforms#00f77b35-fe1d--22feaf783d8fhttps://social./Forums/zh-TW/c6-4d89-995e-1cc4/microsoft-outlook-110-object-library?forum=winforms#00f77b35-fe1d--22feaf783d8fMigrationUser 1https://social.:443/profile/migrationuser%201/?type=forumMicrosoft Outlook 11.0 Object LibraryThat is&probably&because&you&still&aren't&specifing&a&file&name&in&the&SaveAsFile&method...&br/&&br/&Modify&your&code&as&follows:&br/&&br/&oMsg.Attachments.Item(1).SaveAsFile(&C:\Temp\Attach\&&&&oMsg.Attachments.Item(1).FileName)&br/&&br/&This&will&include&a&file&name&to&save&the&attachment&as.&&Make&sure&the&path&&C:\Temp\Attach&&exists.Tue, 25 Jan :25 ZT12:47:25Zhttps://social./Forums/zh-TW/c6-4d89-995e-1cc4/microsoft-outlook-110-object-library?forum=winforms#8cad-4c1e-bed0-f4https://social./Forums/zh-TW/c6-4d89-995e-1cc4/microsoft-outlook-110-object-library?forum=winforms#8cad-4c1e-bed0-f4MigrationUser 1https://social.:443/profile/migrationuser%201/?type=forumMicrosoft Outlook 11.0 Object Librarythanks it&worked.&Is&there&anyway&to&prevent&the&file&from&being&replaced.&SaveAsFile&method&always&overwrites&the&attached.&Is&there&anyway&to&prompt&user&before&overwriting.&br/&&br/&ThanksWed, 26 Jan :20 ZT18:12:20Zhttps://social./Forums/zh-TW/c6-4d89-995e-1cc4/microsoft-outlook-110-object-library?forum=winforms#8def-400d-ba82-bbf10065b9ffhttps://social./Forums/zh-TW/c6-4d89-995e-1cc4/microsoft-outlook-110-object-library?forum=winforms#8def-400d-ba82-bbf10065b9ffMigrationUser 1https://social.:443/profile/migrationuser%201/?type=forumMicrosoft Outlook 11.0 Object LibraryWhy am&i&getting&exception&&Array&index&out&of&bounds&&when&i&try&to&mark&emails&as&read.&code&is&as&follows&br/&&br/&&&&&&&&&&&&&'&Loop&each&unread&message.&br/&&&&&&&&&&&&&Dim&i&As&Integer&br/&&&&&&&&&&&&&For&i&=&1&To&oItems.Count&br/&&&&&&&&&&&&&&&&&'Test&to&make&sure&item&is&a&mail&item&br/&&&&&&&&&&&&&&&&&'and&not&a&meeting&request.&br/&&&&&&&&&&&&&&&&&If&oItems.Item(i).MessageClass&=&sClassComp&Then&br/&&&&&&&&&&&&&&&&&&&&&oMsg&=&oItems.Item(i)&br/&&br/&&&&&&&&&&&&&&&&&&&&&Console.WriteLine(&Subject:&&&+&oMsg.Subject)&br/&&br/&&&&&&&&&&&&&&&&&&&&&'Mark&the&email&as&read.&br/&&&&&&&&&&&&&&&&&&&&&oMsg.UnRead&=&False&br/&&&&&&&&&&&&&&&&&End&If&br/&&&&&&&&&&&&&Next&br/&&br/&&br/&Wed, 26 Jan :40 ZT22:54:40Zhttps://social./Forums/zh-TW/c6-4d89-995e-1cc4/microsoft-outlook-110-object-library?forum=winforms#000b9e92-c427-40b8-958b-40a58d62c595https://social./Forums/zh-TW/c6-4d89-995e-1cc4/microsoft-outlook-110-object-library?forum=winforms#000b9e92-c427-40b8-958b-40a58d62c595MigrationUser 1https://social.:443/profile/migrationuser%201/?type=forumMicrosoft Outlook 11.0 Object LibraryI didn't&explicitly&mention&it&in&the&last&post,&but&you&do&need&to&be&looping&through&the&attachments&collection&and&saving&each&one,&so&your&code&should&use&&i&&instead&of&&1&.&&&1&&was&just&the&correct&value&for&the&original&posted&code.&br/&&br/&I&assume&you're&doing&something&like:&br/&&br/&For&i&as&Integer&=&1&to&oMsg.Attachments.Count&br/&&br/&oMsg.Attachments.Item(i).SaveAsFile(&C:\Temp\Attach\&&&&oMsg.Attachments.Item(i).FileName)&&br/&&br/&Next&br/&&br/&Note&however&that:&br/&&br/&&br/&&&&&&&&&&&&&Dim&enm&As&IEnumerator&=&oMsg.Attachments.GetEnumerator&br/&&&&&&&&&&&&&While&enm.MoveNext&br/&&&&&&&&&&&&&&&&&Dim&a&As&Outlook.Attachment&=&enm.Current&br/&&&&&&&&&&&&&&&&&a.SaveAsFile(&c:\temp\attach\&&&&a.FileName)&br/&&br/&&&&&&&&&&&&&End&While&br/&&br/&&br/&Is&much&better&than&a&For-Next&loop.&&And&you&could&even&trim&that&down&to&simply:&br/&&br/&enm.Current.SaveAsFile(&c:\temp\attach\&&&&enm.Current.FileName)&br/&&br/&or,&if&you&want&the&intellisense:&br/&&br/&cType(enm.Current,&Oulook.Attachment).SaveAsFile(&c:\temp\attach\&&&&cType(enm.Current,&Outlook.Attachment).FileName)&br/&&br/&As&long&as&your&using&the&FileName&of&the&current&attachment&you&are&saving,&you&should&not&have&an&overwrite&issue&unless&you've&previously&saved&an&attachment&with&the&same&name.&&If&you&find&you&are&repeatedly&saving&attachments&(from&different&emails)&with&the&same&filename,&you&should&then&first&create&a&new&subdirectory&in&c:\temp\attach.&&You&could&use&info&parsed&from&the&email&to&name&the&subdirectory,&or&simply&count&the&number&of&directories&in&c:\temp\attach&and&add&one&named&whatever&your&folder&count&was.&br/&Thu, 27 Jan :59 ZT15:22:59Zhttps://social./Forums/zh-TW/c6-4d89-995e-1cc4/microsoft-outlook-110-object-library?forum=winforms#-3e84-45f5-be3c-7b595a7ea56bhttps://social./Forums/zh-TW/c6-4d89-995e-1cc4/microsoft-outlook-110-object-library?forum=winforms#-3e84-45f5-be3c-7b595a7ea56bMigrationUser 1https://social.:443/profile/migrationuser%201/?type=forumMicrosoft Outlook 11.0 Object LibraryI'm not&sure&why&you're&getting&that&exception,&but&again,&if&you&use&an&Enumerator&rather&than&a&For-Next&loop,&you&won't&be&able&to&go&out&of&bounds.&br/&&br/&Ex:&br/&&br/&'&Loop&each&unread&message.&&br/&Dim&enm&As&IEnumerator&=&oItems.GetEnumerator&br/&While&enm.MoveNext()&br/&'Test&to&make&sure&item&is&a&mail&item&&br/&'and&not&a&meeting&request.&&br/&If&enm.Current.MessageClass&=&sClassComp&Then&&br/&oMsg&=&enm.Current&br/&&br/&Console.WriteLine(&Subject:&&&+&oMsg.Subject)&&br/&&br/&'Mark&the&email&as&read.&&br/&oMsg.UnRead&=&False&&br/&End&If&&br/&End&While&br/&&br/&Hope&that&helps!Thu, 27 Jan :12 ZT15:26:12Zhttps://social./Forums/zh-TW/c6-4d89-995e-1cc4/microsoft-outlook-110-object-library?forum=winforms#c6c57f30-ff11-4b31-b6ae-d9bhttps://social./Forums/zh-TW/c6-4d89-995e-1cc4/microsoft-outlook-110-object-library?forum=winforms#c6c57f30-ff11-4b31-b6ae-d9bMigrationUser 1https://social.:443/profile/migrationuser%201/?type=forumMicrosoft Outlook 11.0 Object Librarythanks rkimbleSun, 30 Jan :30 ZT21:37:30Zhttps://social./Forums/zh-TW/c6-4d89-995e-1cc4/microsoft-outlook-110-object-library?forum=winforms#d6e59dd4--a210-8efe526a74cbhttps://social./Forums/zh-TW/c6-4d89-995e-1cc4/microsoft-outlook-110-object-library?forum=winforms#d6e59dd4--a210-8efe526a74cbMigrationUser 1https://social.:443/profile/migrationuser%201/?type=forumMicrosoft Outlook 11.0 Object LibraryNo problem.&&Good&luck!Mon, 31 Jan :47 ZT13:31:47Z}

我要回帖

更多关于 write和writeline 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信