Solved

File Exists Always fails

Posted on 2011-09-28
10
294 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

832 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