无法在C#.net文件上传中声明静态类型为'System.IO.File'的变量 [英] Cannot declare a variable of static type 'System.IO.File' in c# .net file upload

查看:1093
本文介绍了无法在C#.net文件上传中声明静态类型为'System.IO.File'的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,请帮助我.....我正在使用的代码是



Scripting.FileSystemObjectClass f =新的Scripting.FileSystemObjectClass();
Scripting.Folder Fld;
System.IO.File fl =默认值(System.IO.File);
Excel.Application xlapp =新的Excel.Application();
Excel.Worksheet xlsheet =新的Excel.Worksheet();

但我得到一个错误是无法声明静态类型为'System.IO.File'的变量"
请帮助我

hello pls help me..... i am using the code is



Scripting.FileSystemObjectClass f = new Scripting.FileSystemObjectClass();
Scripting.Folder Fld;
System.IO.File fl = default(System.IO.File);
Excel.Application xlapp = new Excel.Application();
Excel.Worksheet xlsheet = new Excel.Worksheet();

but i got a error is " Cannot declare a variable of static type ''System.IO.File''"
please help me

推荐答案

是的,这种类型是静态的.不仅您不能创建此类的实例,而且显然您也永远不需要它. :-)

如果您尝试像System.IO.File fl = default(System.IO.File);这样的行,那么您显然不知道该类是什么,无论是静态的还是非静态的.在这里,除了系统地研究编程,特别是对OOP之外,没有任何帮助.尝试获取基本的语言和.NET手册,并从头开始阅读 ,并随心所欲地进行练习,直到问题已经解决为止. 别无他法.

只是一个提示:您永远不需要甚至不存在的fl.取而代之的是,您始终应使用System.IO.File.*,其中"*"是此类的任何方法或属性.但是,如果您不了解自己在做什么,就不应该这样做.而且您显然缺乏这种了解,因此...请参见上文.

同时,请看一下我过去的解决方案,详细解释此问题:
什么使静态方法可访问? [
Yes, this type is static. Not only you cannot create and instance of such type, but apparently you also never need it. :-)

If your are trying to suck line like System.IO.File fl = default(System.IO.File);, you apparently have no idea what the class is, no matter static or non-static. Nothing can help here but systematic study of programming in general and OOP in particular. Try to get to a basic language and .NET manual and read it from the very beginning, doing exercise as you go, until the problem has gone too far. There is no other way.

Just a hint: you never need your fl, which cannot even exist. Instead of it, you always should use System.IO.File.*, where ''*'' is any method or property of this class. But you should not do it without understanding of what are you doing; and you apparently lack this understanding, so… see above.

In the meanwhile, just take a look at my past solution explaining this matter in detail:
What makes static methods accessible?[^].

It might at least give you an idea on what''s involved. You may or may not understand it though. Than again… see above.

Good luck,
—SA


好吧,它以非常简洁明了的方式描述了错误的性质:您无法声明变量来保存对静态类的实例,因为首先不能实例化静态类.

我认为您正在寻找的是 FileStream 类,您可以通过多种方式获得该类的实例;使用适当的参数调用 File.Open()就是其中之一.
Well, that pretty much describes the nature of the error in a very neat and explicit fashion: you cannot declare a variable to hold a reference to an instance of static class, because static classes cannot be instantiated, in the first place.

I think what you''re looking for is the FileStream class, you can get an instance of this class in several ways; calling File.Open() with the appropriate arguments is one of them.


这篇关于无法在C#.net文件上传中声明静态类型为'System.IO.File'的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆