Solved

File Exists Always fails

Posted on 2011-09-28
10
296 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
  • 6
  • 2
  • 2
10 Comments
 
LVL 17

Accepted Solution

by:
sonawanekiran earned 250 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 250 total points
ID: 36812875
Have you tried with Server.MapPath

_filename = Server.MapPath("~/Models/TrendEventsDatabase.xml");
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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 250 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 250 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

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.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

821 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