Solved

File Exists Always fails

Posted on 2011-09-28
10
298 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 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

Independent Software Vendors: 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!

Question has a verified solution.

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

A short article about problems I had with the new location API and permissions in Marshmallow
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
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 …

738 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