Solved

File Exists Always fails

Posted on 2011-09-28
10
292 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:hbash
  • 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:hbash
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
 
LVL 1

Author Comment

by:hbash
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 1

Author Comment

by:hbash
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:hbash
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:hbash
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:hbash
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This article will show, step by step, how to integrate R code into a R Sweave document
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
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 …

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now