Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hey all,

Looking to access files in the bin folder of my C# project (Win app) and want to do this using an openfiledialog box. How to I get the dialog box to point straight to the bin folder without using the line @"C\\...... Need to be able to port my app to other machines....

 

Any help would be appreciated!

 

Cheers

Ziggy.

Posted

I don't have VS.Net open at the moment, but if memory serves:

 

Application.StartupPath returns the string for the path from where your application was started. There is another one as well, but this is the one I use most often.

Posted

You would just do something like:

 


string AppPath = Application.Startup+"\\<filename.ext>"[/Code]

 

I hope I got the C# right - I havn't used it in a while. I've been more VB recently.

 

From what I recall, the Open Dialog Box only lets people navigate around to find a path/file and then passes the Path back to your class.

 

With Application.StartupPath and knowing the directory structure of your application and filenames you can do all this via code completely and automatically.

Posted
If path1 does not end with a valid separator character as defined in DirectorySeparatorChar, AltDirectorySeparatorChar, or VolumeSeparatorChar, DirectorySeparatorChar is appended to path1 before concatenation.

is number one

 

 

using Application.StartupPath is ok, but as a general practice, when contactonating strings to create file paths in the .net framework, it's better to use Path.Combine.

 

e.x.:

string AppPath = Application.Startup+"\\<filename.ext>"

turns to

string AppPath = Path.Combine(Application.Startup, "filename.ext")

Posted
is number one

 

 

using Application.StartupPath is ok, but as a general practice, when contactonating strings to create file paths in the .net framework, it's better to use Path.Combine.

 

e.x.:

string AppPath = Application.Startup+"\\<filename.ext>"

turns to

string AppPath = Path.Combine(Application.Startup, "filename.ext")

 

 

Ok. I"m not doubting you - I just don't like it when people make a broad statement like "X is better than Y" without giving reasons.

 

Using Application.StartupPath you *do* have to remember to put in the "\" which is a little annoying.

 

So you're replacing "\" with Path.Combine( , )? I'm not sure that it's "Better". It certainly is a lot longer and I don't see any benifits to doing so unless you can't remember what identifier goes between the filename and the path.

 

Now what would be more helpful is to overload it to take only one parameter - the filename and it will look like:

 

Path.Combine("filename.ext") and that will be an improvement.

Posted

Sorry to intrude in this debate but�

 

Path.Combine is better as using path & �\� & filename for the following easy reason:

 

Let�s say in the future the .net framework gets ported to linux and you want to use your program there, Path combine will insert the correct separator in the path. Linux uses / instead of \ Even worse I have no clue how that paths will be represented in the future versions of windows

 

I also noticed (probably while being drunk one night) I typed in a path constant: �c:/test/� using the path class this automatically gets converted into �c:\test\�. So when connecting to a bizar network that uses something else as path separator the framework will fix this for you.

 

The only reason I would use path & �\� & filename is to gain speed. Path.combine is slower; if I have to combine a few hundred thousands of paths in a loop I might be tempted to use the fast over the slow technique even if that breaks cross platform compatibility.

Posted
Sorry to intrude in this debate but�

 

Path.Combine is better as using path & �\� & filename for the following easy reason:

 

Let�s say in the future the .net framework gets ported to linux and you want to use your program there, Path combine will insert the correct separator in the path. Linux uses / instead of \ Even worse I have no clue how that paths will be represented in the future versions of windows

 

Perfect. Actually the .Net framework is ported over to Linux and OS X and I'm currently working on an application I want to work across the platforms. This would have certainly hit me when I tested it on another platform.

 

I also noticed (probably while being drunk one night) I typed in a path constant: �c:/test/� using the path class this automatically gets converted into �c:\test\�. So when connecting to a bizar network that uses something else as path separator the framework will fix this for you.

 

Drinking and programming - while fun - can be hazardous to your health, but that looks like a good option.

 

Thank you for explaining things and putting it into perspective. I question to gain perspective and insight, not to be annoying or a jerk.

 

I'm going to use Path.Combine from now on. :D

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...