FileNotFoundException was unhandled by user code

racineconde
racineconde used Ask the Experts™
on
I am trying to read documents from a SharePoint Foundation web site from a console application (I have the same error from an ASP .NET web application).

I get an "FileNotFoundException was unhandled by user code" error message. The detail of the message is:
************************************************************************
The Web application at http://win2008 could not be found. Verify that you have typed the URL correctly. If the URL should be serving existing content, the system administrator may need to add a new request URL mapping to the intended application.
************************************************************************

Here is my code:
**************************************************************************************
            using (SPWeb oWebsite = new SPSite("http://win2008").OpenWeb())
            {
                string folderUrl = "Shared Documents";
                SPFolder oFolder = oWebsite.GetFolder(folderUrl);
                SPFileCollection collFile = oFolder.Files;

                foreach (SPFile oFile in collFile)
                {
                    Console.WriteLine(oFile.Name);
                 }
            }
***********************************************************************************

Thanks!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
The code  must run on the SharePoint sever.  it won't work anywhere else.  Since you say you connected  remotely to the server   I'm not clear on where you are trying to run your code. Since this is for 2010 make sure you set the project to .net 3.5 and  AnyCPU.  It will tend to default to .net 4.0 and x86 CPU.  The New  Site(url) command won't work in a project that defaults to x86 CPU as  the target.  This is a known issue  in VS 2010. With the using statements there is no need to close  the Web or site.  It will happen automatically.  
Otherwise I don't really see a problem with your code.

http://blogs.msdn.com/b/manuj/archive/2010/06/19/the-web-application-at-xxx-could-not-be-found-when-using-sharepoint-om-in-an-asp-net-webapplication-on-64-bit-server.aspx

http://social.technet.microsoft.com/Forums/en-CA/sharepoint2010programming/thread/faa25866-330b-4e60-8eee-bd72dc9fa5be

Author

Commented:
The application is running on the Sharepoint server.
I've tried the modifications you (and the links you pointed me to) have suggested but I'm still getting the same error...

Author

Commented:
Yes I am.

I need to point out though that when \i tried to change the platform target, the only choice I had in the drop down list was "x86". The other options in the list were "<New>" and "<Edit>".
I created an "Any CPU" option and used it.

Author

Commented:
I had to do some other research to make this work

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial