Opening a File by using the extension?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • user033088
    New Member
    • Mar 2013
    • 25

    Opening a File by using the extension?

    The code below is used to return a file path.
    Code:
     private string BrowseJournalFile(string startRelativePath)
            {
                string str = "Journal files (*.log)|*.log|All files (*.*)|*.*";
                return PathTools.BrowseFile(startRelativePath, str, this.RootPath);
    
    
            }
    So once I click browse I get something like "E:\\New Files"
    But there is this .log file in this folder which I want to automatically store in this path.
    Right now I have to click on this .log file and then select open.
    Is there a way I can directly load the file.
    I have been trying to use the FileStream but no luck.
    Thank You in advance!!
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    Well, what do you do with the path that gets returned from the file browser?

    Comment

    • user033088
      New Member
      • Mar 2013
      • 25

      #3
      Now, what I do is, Click on Browse ( E:\\New Files\\Folder1) then I click on the ABC.log file in that path.
      Instead is there a way I could just get E:\\New Files\\Folder1\ \ABC.log without having to browse and click on the file? Here the Folder1 will keep on changing. It could be Folder2 and so on.
      Thank You for replying !!

      Comment

      • vijay6
        New Member
        • Mar 2010
        • 158

        #4
        Hey user033088, try this code...

        Code:
        MessageBox.Show(System.IO.Directory.GetFiles(Application.StartupPath, "*.log").FirstOrDefault().ToString());

        If your application is running in "E:\New Files\Folder1\" directory means the above code will return the log file which is placed on that directory.

        Comment

        • AceInfinity
          New Member
          • Apr 2013
          • 12

          #5
          @vijay6 - If you're going to use FirstOrDefault( ) but not check whether a result was found, then why not just use First()? Another thing about your code is that the ToString() is redundant...

          Also, @user033088, are you trying to load this file or just dynamically get the filepath? Have you tried a StreamReader?

          Comment

          • vijay6
            New Member
            • Mar 2010
            • 158

            #6
            If you're going to use FirstOrDefault( ) but not check whether a result was found, then why not just use First()?
            Hey @AceInfinity, read the question again. There is not need to check whether the log is present or not. Because already a log file is present on that directory. In this scenario both the properties (First() or FirstOrDefault( )) ll return the same answer.

            Another thing about your code is that the ToString() is redundant...
            Yes, you're right. I didn't noticed this one...

            Comment

            • AceInfinity
              New Member
              • Apr 2013
              • 12

              #7
              First() and FirstOrDefault( ) are not properties. Also whether it exists or not is irrelevent, and so is the question. I don't think you're understanding what I was trying to point out though.

              Your code here is the only thing I was basing my statement off of. There is a reason why one is called FirstOrDefault(), and the other is just called First().

              You don't take into account the default return value for FirstOrDefault( ) and you use FirstOrDefault( ) as though to assume that there will always be something "found" by this query. If you're going to assume that you will always have at least one element to be able to return the First element of a query, without the LINQ query itself returning no elements, then you could just use First().

              edit: Look...
              Code:
              char var1 = "abcd".FirstOrDefault(c => c == 'x');
              Console.WriteLine(var1 == null); // because x doesn't exist in that string...
              char nvar2 = "abcd".First(c => c == 'x');
              Console.WriteLine(var1 == null); // this part throws an exception because it expects that there will be a value...
              Have you read the MSDN docs for FirstOrDefault( ) vs. First()?

              There is not need to check whether the log is present or not.
              If that is true, then there's no need for FirstOrDefault( ), and you should instead probably just use First() if you're going to use LINQ here..

              NOTE: *Both First() and FirstOrDefault( ) are going to be slower than indexing though so why not just index 0 directly??

              Example:
              Code:
              Directory.GetFiles("Filepath")[0];
              Last edited by AceInfinity; Apr 29 '13, 10:38 AM. Reason: Added demo code

              Comment

              • vijay6
                New Member
                • Mar 2010
                • 158

                #8
                Hey @AceInfinity, got ya!!! Thanks for your information.

                Comment

                Working...