Click here to Skip to main content
15,915,848 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
Posted
Updated 4-Jun-20 22:45pm

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
 
Share this answer
 
v2
Comments
LosBear 11-May-12 13:19pm    
Little harsh, don't you think?
He's posting his question 'cause he needs help, not to be berated. Still, you somewhat explain address the question, so I'm obligated to give you thumbs up =)
Sergey Alexandrovich Kryukov 11-May-12 13:48pm    
Thank you.
Harsh? maybe. You say it as it's something bad. :-)
You know, I think this is the best help I could think of. After all, what's more important: to tell the truth or please someone? If both can be done, great, but what if the truth is not very pleasant. In some situation, I feel if I won't tell it, nobody will...
--SA
Member 10261821 20-May-14 12:31pm    
how arrogant an answer can be...
Sergey Alexandrovich Kryukov 20-May-14 13:50pm    
In some cases, it's important to talk this way. Let me ask you: would you rather prefer lie? :-)
—SA
Member 9962156 27-Jan-16 22:53pm    
I know where you're coming from but honestly you're going to turn people away from asking questions and learning - not only the people you respond to in this fashion but all the people who are coming here for an answer, seeing things like this and saying "i'm out".

Don't ever forget where you came from - you did not come out of your mother writing code. Whether you learned on your own or someone taught you - I will bet my life you've asked someone a question about something you did not understand. Now imagine they made you feel stupid for asking and not much else. maybe you wouldn't know all the incredible things you know today.
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900