Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

File Exists Always fails

Posted on 2011-09-28
10
Medium Priority
?
306 Views
Last Modified: 2013-12-17
I have a function in a Silverlight application that always fails and I do not see why:  I have pasted the routine for review.

Thanks.
// Retrieves evnets
        public IEnumerable<TrendEvent> GetEvents()
        {
            //FileInfo fi = new FileInfo(_filename);
            //bool exists = fi.Exists;

            if (!File.Exists(_filename))
            {
                throw new System.IO.FileNotFoundException("DataSource file could not be found");
            }

            var allevents = System.Xml.Linq.XDocument.Load(_filename).Root.Elements("Event").Select(
                x => new TrendEvent(
                    (int)x.Element("EVENT_ID"),
                    (int)x.Element("CUSTOMER_ID"),
                    (int)x.Element("ElapsedTime"),
                    (string)x.Element("Comments"),
                    (DateTime)x.Element("CreatedDate")
                    ));

            return allevents;
        }

Open in new window

0
Comment
Question by:Howard Bash
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 2
  • 2
10 Comments
 
LVL 17

Accepted Solution

by:
sonawanekiran earned 1000 total points
ID: 36812794
Check _filename path    

 // See if this file exists in the SAME DIRECTORY.
      if (File.Exists("TextFile1.txt"))
      {
          // File exists
      }
      
      // See if this file exists in the C:\ drive directory [Note the '\\' part]
      if(File.Exists("C:\\lost.txt"))
      // exists
0
 
LVL 1

Author Comment

by:Howard Bash
ID: 36812828
Here is the file variable.
        private static readonly string _filename = @"Models\TrendEventsDatabase.xml";

There is a Models folder within the project which contains the file.  It seems like a rights issue.
0
 
LVL 17

Assisted Solution

by:sonawanekiran
sonawanekiran earned 1000 total points
ID: 36812875
Have you tried with Server.MapPath

_filename = Server.MapPath("~/Models/TrendEventsDatabase.xml");
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:Howard Bash
ID: 36812900
These two lines:
            FileInfo fi = new FileInfo(_filename);
            bool exists = fi.Exists;

Produce the following exception which really sounds like a rights issues.

System.Security.SecurityException was unhandled by user code
  Message=File operation not permitted. Access to path 'Models\TrendEventsDatabase.xml' is denied.
  StackTrace:
       at System.IO.FileSecurityState.EnsureState()
       at System.IO.FileInfo.Init(String fileName, Boolean checkHost)
       at System.IO.FileInfo..ctor(String fileName)
       at NFSilverlightApplicationMVVM.Models.TrendEventsRepository.GetEvents()
       at NFSilverlightApplicationMVVM.Views.MainPage..ctor()
       at NFSilverlightApplicationMVVM.App.Application_Startup(Object sender, StartupEventArgs e)
       at MS.Internal.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args)
       at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)
  InnerException:
0
 
LVL 8

Assisted Solution

by:jagrut_patel
jagrut_patel earned 1000 total points
ID: 36812902
Just to verify whether it is a rights issue you can run your application in Out-of-Browser mode and test whether File.Exists works or not.

I am wondering if it is SL how can it find a relative path like this!
0
 
LVL 1

Author Comment

by:Howard Bash
ID: 36812937
Can you explain how to run out of browser mode?
0
 
LVL 8

Assisted Solution

by:jagrut_patel
jagrut_patel earned 1000 total points
ID: 36812953
1. Right-click your SL project node in Solution Explorer
2. Select Properties
3. In the Silverlight tab, check the "Enable running application Out of the Browser" checkbox.
4. To run you app with Elevated trust, click Out-of-Browser settings button
5. Check the "required elevated trust..." checkbox

Run your app.

1. In the browser right click the SL plugin
2. Select "Install <your app> on to this computer"
0
 
LVL 1

Author Comment

by:Howard Bash
ID: 36812970
After I set to run out of browser I am getting a message that my wcf ria service will fail and if I want to continue.  I told the dialog I do and it proceeded to fail.
0
 
LVL 1

Author Comment

by:Howard Bash
ID: 36812992
Actually I think the wcf ria msg is related to my having added and then deleted a wcf ria project from this solution.  sometimes added components aren't that easy to get rid of such as this one I think.
0
 
LVL 1

Author Comment

by:Howard Bash
ID: 36815639
Ok.  I rebuilt the application.  and now I get that wcf ria service error mentioned above.  It began after I right clicked the SL app and enabled out of browser checkbox and checking the required elevated trust.

This error persists even when I uncheck these two options.  It seems to make a change that does not back out.  Not a good behavior.
0

Featured Post

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question