473,386 Members | 2,042 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Opening a File by using the extension?

The code below is used to return a file path.
Expand|Select|Wrap|Line Numbers
  1.  private string BrowseJournalFile(string startRelativePath)
  2.         {
  3.             string str = "Journal files (*.log)|*.log|All files (*.*)|*.*";
  4.             return PathTools.BrowseFile(startRelativePath, str, this.RootPath);
  5.  
  6.  
  7.         }
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!!
Apr 24 '13 #1

✓ answered by AceInfinity

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...
Expand|Select|Wrap|Line Numbers
  1. char var1 = "abcd".FirstOrDefault(c => c == 'x');
  2. Console.WriteLine(var1 == null); // because x doesn't exist in that string...
  3. char nvar2 = "abcd".First(c => c == 'x');
  4. 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:
Expand|Select|Wrap|Line Numbers
  1. Directory.GetFiles("Filepath")[0];

7 1410
Rabbit
12,516 Expert Mod 8TB
Well, what do you do with the path that gets returned from the file browser?
Apr 24 '13 #2
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 !!
Apr 24 '13 #3
vijay6
158 100+
Hey user033088, try this code...

Expand|Select|Wrap|Line Numbers
  1. 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.
Apr 25 '13 #4
@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?
Apr 29 '13 #5
vijay6
158 100+
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...
Apr 29 '13 #6
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...
Expand|Select|Wrap|Line Numbers
  1. char var1 = "abcd".FirstOrDefault(c => c == 'x');
  2. Console.WriteLine(var1 == null); // because x doesn't exist in that string...
  3. char nvar2 = "abcd".First(c => c == 'x');
  4. 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:
Expand|Select|Wrap|Line Numbers
  1. Directory.GetFiles("Filepath")[0];
Apr 29 '13 #7
vijay6
158 100+
Hey @AceInfinity, got ya!!! Thanks for your information.
Apr 29 '13 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Vilmar Brazão de Oliveira | last post by:
Hi, How verify type or extension of file using ASPUPLOAD? I check documentation, but I didn't find anything else yet. thanks, -- ««««««««»»»»»»»»»»»»»» Vlmar Brazão de Oliveira...
0
by: Stephen | last post by:
could someone give me some advise on a problem I have. At present i have a web application which displays pdf files in a web browser using javascript and the window.open method and pointing to a...
0
by: Ganesh Kolappan via .NET 247 | last post by:
Hi I am trying to populate a <asp:dropdownlist> in a XSLT file withdatasource pointing to a C# codebehind file method which returnsa dataview. I am using XSLT extension object. But I am...
3
by: Ben | last post by:
Hi all - I am having a bit of trouble and thought maybe someone in this group could shed some light. Here's the skinny... I am creating an automated process to import a bunch of text files...
28
by: Madhur | last post by:
Hello what about this nice way to open a file in single line rather than using if and else. #include<stdio.h> void main() { FILE *nd; clrscr();...
1
by: D. Yates | last post by:
Hi, I am looking for an example of how to extract bitmap images from an embedded resource file (a file with *.res extension, which can be viewed inside of the ide and can hold bitmaps, icons,...
4
by: Pieter | last post by:
Hi, From my VB.NET 2005 application I need to be able to open Files an Directory's with their default application: - A word-doc must open in word - A excell-document in Excel etc - in case...
2
by: OutdoorGuy | last post by:
Greetings, I have a "newbie" question in relation to opening files from C#. I have a Windows form where I allow the user to type in a file extension in a text box (e.g., "xls"). I then take...
2
by: quicklearner | last post by:
hi I have made a CHM file and I have linked my .CHM file with the controls on the Form in my application which is made in C# 2005. Its working fine . But I want that if user presses 'F1' then when...
3
Looking2Learn
by: Looking2Learn | last post by:
Hey, I'm trying to open a txt file, but I won't know the first three characters of the filename. For example, I want to open a "user.txt" file, and I know it will have three digits in front of it,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.