Strinp the name
Messages   Related Types
This message was discovered on ASPFriends.com 'ngfx-io' list.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.

Carlos Magalhaes
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=us-ascii">
<META NAME="Generator" CONTENT="MS Exchange Server version 5.5.2652.35">
<TITLE>Strinp the name</TITLE>
</HEAD>
<BODY>

<P><FONT SIZE=2>Hi all,</FONT>
</P>

<P><FONT SIZE=2>You know you get the files full path then you get the files name on its own e.g.</FONT>
</P>

<P><FONT SIZE=2>Path: \\server\share\filename.name</FONT>
</P>

<P><FONT SIZE=2>File name : filename.name</FONT>
</P>

<P><FONT SIZE=2>Now I know how to get both BUT how can I strip the extension from it so that I just get the name e.g.</FONT>
</P>

<P><FONT SIZE=2>File Name = Filename and not Filename.name</FONT>
</P>

<P><FONT SIZE=2>Thanks</FONT>
</P>

<P><FONT SIZE=2>Carlos</FONT>
</P>

</BODY>
</HTML>

-------------------------------------------------------------
This email and any files transmitted are
confidential and intended solely for the
use of the individual or entity to which
they are addressed, whose privacy
should be respected. Any views or
opinions are solely those of the author
and do not necessarily represent those
of the Trencor Group, or any of its
representatives, unless specifically
stated.

Email transmission cannot be guaranteed
to be secure, error free or without virus
contamination. The sender therefore
accepts no liability for any errors or
omissions in the contents of this message,
nor for any virus infection that might result
from opening this message. Trencor is not
responsible in the event of any third party
interception of this email.

If you have received this email in error please notify
Click here to reveal e-mail address For more information about
Trencor, visit www.trencor.net <http://www.trencor.net>

Reply to this message...
 
    
Mitch Denny (VIP)
Carlos,

A fairly inefficient way of doing it would be to construct
an instance of the FileInfo class and use its extension
property to get at the value:

    string filename = @"C:\PAGEFILE.SYS";
    string extension = new FileInfo(filename).Extension;

I'm not 100% sure but I think that gets a handle on the
file which may not be desirable, so you might want to do
some simple string manipulation instead which is basically
the process of determining if there is an extension by
seeing if a period (.) exists in a string and getting
the characters after it to see if some might indicate
that it isn't an extension after all. Here is a method
that takes a good stab of at resolving the extension
reliably:

private static string GetExtension(string filename)
{

// Declare locals.
char[] invalidCharacters = null;
int invalidCharacterIndex = 0;
int periodIndex = 0;
string extension = null;

// Determine if there is a period in the
// provided filename string.
periodIndex = filename.LastIndexOf(".");
if (periodIndex == -1)
{

// If there is not period in the string
// then there is definately no extension
// so set the return value to null.
extension = null;

}
else
{

// It looks like there could possibly be an extension,
// pull it out from the filename for closer examination.
extension = filename.Substring(periodIndex + 1);

// Build up a list of invalid characters for the
// extension that would indicate that the period
// where we split on wasn't an extension delimeter.
invalidCharacters = new char[1]
{
'\\'
};

// Check to see if any of the invalid characters are in the
string.
invalidCharacterIndex =
extension.LastIndexOfAny(invalidCharacters);
if (invalidCharacterIndex != -1)
{

// Since an invalid character is in the string
// then null out the extension local since it
// obviously isn't a real extension.
extension = null;

}

}

// Return result.
return extension;

}

Hope this helps.

----------------------------------------
- Mitch Denny
- Click here to reveal e-mail address
- +61 (414) 610-141
-

-----Original Message-----
From: Carlos Magalhaes [mailto:Click here to reveal e-mail address]
Sent: Friday, 7 June 2002 00:25
To: ngfx-io
Subject: [ngfx-io] Strinp the name

Hi all,
You know you get the files full path then you get the files name on its
own e.g.
Path: \\server\share\filename.name
File name : filename.name
Now I know how to get both BUT how can I strip the extension from it so
that I just get the name e.g.
File Name = Filename and not Filename.name
Thanks
Carlos
| [ngfx-io] member Click here to reveal e-mail address = YOUR ID |
http://www.aspfriends.com/aspfriends/ngfx-io.asp = JOIN/QUIT
------------------------------------------------------------- This email
and any files transmitted are confidential and intended solely for the
use of the individual or entity to which they are addressed, whose
privacy should be respected. Any views or opinions are solely those of
the author and do not necessarily represent those of the Trencor Group,
or any of its representatives, unless specifically stated. Email
transmission cannot be guaranteed to be secure, error free or without
virus contamination. The sender therefore accepts no liability for any
errors or omissions in the contents of this message, nor for any virus
infection that might result from opening this message. Trencor is not
responsible in the event of any third party interception of this email.
If you have received this email in error please notify
Click here to reveal e-mail address For more information about Trencor, visit
www.trencor.net

Reply to this message...
 
    
Jon Gilkison
[Original message clipped]

Why not use:

string extension=Path.Extension(filename);
string filename=Path.GetFileNameWithoutExtension(filename);

Jon Gilkison
//interfacelab.com/

Reply to this message...
 
    
Mitch Denny (VIP)
Jon,

Thanks! I am glad you posted, I didn't know about the
Path class, just goes to show its difficult to be
across the whole framework :)

----------------------------------------
- Mitch Denny
- Click here to reveal e-mail address
- +61 (414) 610-141
-

-----Original Message-----
From: Jon Gilkison [mailto:Click here to reveal e-mail address]
Sent: Friday, 7 June 2002 11:36
To: ngfx-io
Subject: [ngfx-io] RE: Strinp the name

[Original message clipped]

Why not use:

string extension=Path.Extension(filename);
string filename=Path.GetFileNameWithoutExtension(filename);

Jon Gilkison
//interfacelab.com/

| [ngfx-io] member Click here to reveal e-mail address = YOUR ID
| http://www.aspfriends.com/aspfriends/ngfx-io.asp = JOIN/QUIT

Reply to this message...
 
    
Carlos Magalhaes
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV=3D"Content-Type" CONTENT=3D"text/html; =
charset=3Dus-ascii">
<META NAME=3D"Generator" CONTENT=3D"MS Exchange Server version =
5.5.2652.35">
<TITLE>RE: [ngfx-io] RE: Strinp the name</TITLE>
</HEAD>
<BODY>

<P><FONT SIZE=3D2>Ahh great stuff that will help ! Thanks Jon! Is there =
no way of taking that extension and finding out what the relevant =
program is for that extension so one can display it e.g. if the =
extension is .doc then display word if you cant find the program =
associated with the extension the display no program =
found....</FONT></P>
<BR>

<P><FONT SIZE=3D2>Any ideas?</FONT>
</P>

<P><FONT SIZE=3D2>-----Original Message-----</FONT>
<BR><FONT SIZE=3D2>From: Mitch Denny [<A =
HREF=3D"mailto:Click here to reveal e-mail address">mailto:Click here to reveal e-mail address</=
A>] </FONT>
<BR><FONT SIZE=3D2>Sent: Friday, June 07, 2002 4:06 AM</FONT>
<BR><FONT SIZE=3D2>To: ngfx-io</FONT>
<BR><FONT SIZE=3D2>Subject: [ngfx-io] RE: Strinp the name</FONT>
</P>

<P><FONT SIZE=3D2>Jon,</FONT>
</P>

<P><FONT SIZE=3D2>Thanks! I am glad you posted, I didn't know about =
the</FONT>
<BR><FONT SIZE=3D2>Path class, just goes to show its difficult to =
be</FONT>
<BR><FONT SIZE=3D2>across the whole framework :)</FONT>
</P>

<P><FONT SIZE=3D2>----------------------------------------</FONT>
<BR><FONT SIZE=3D2>- Mitch Denny</FONT>
<BR><FONT SIZE=3D2>- Click here to reveal e-mail address</FONT>
<BR><FONT SIZE=3D2>- +61 (414) 610-141</FONT>
<BR><FONT SIZE=3D2>-</FONT>
</P>

<P><FONT SIZE=3D2>-----Original Message-----</FONT>
<BR><FONT SIZE=3D2>From: Jon Gilkison [<A =
HREF=3D"mailto:Click here to reveal e-mail address">mailto:jon.gilkison@interf=
acelab.com</A>] </FONT>
<BR><FONT SIZE=3D2>Sent: Friday, 7 June 2002 11:36</FONT>
<BR><FONT SIZE=3D2>To: ngfx-io</FONT>
<BR><FONT SIZE=3D2>Subject: [ngfx-io] RE: Strinp the name</FONT>
</P>
<BR>

<P><FONT SIZE=3D2>> Now I know how to get both BUT how can I strip =
the extension</FONT>
<BR><FONT SIZE=3D2>> from it so that I just get the name e.g. =
</FONT>
<BR><FONT SIZE=3D2>> File Name =3D Filename and not Filename.name =
</FONT>
</P>

<P><FONT SIZE=3D2>Why not use:</FONT>
</P>

<P><FONT SIZE=3D2>string extension=3DPath.Extension(filename);</FONT>
<BR><FONT SIZE=3D2>string =
filename=3DPath.GetFileNameWithoutExtension(filename);</FONT>
</P>

<P><FONT SIZE=3D2>Jon Gilkison</FONT>
<BR><FONT SIZE=3D2>//interfacelab.com/</FONT>
</P>
<BR>

<P><FONT SIZE=3D2>| [ngfx-io] member Click here to reveal e-mail address =3D YOUR =
ID </FONT>
<BR><FONT SIZE=3D2>| <A =
HREF=3D"http://www.aspfriends.com/aspfriends/ngfx-io.asp"" target="_blank">http://www.aspfriends.com/aspfriends/ngfx-io.asp"; =
TARGET=3D"_blank">http://www.aspfriends.com/aspfriends/ngfx-io.asp</A> =
=3D JOIN/QUIT</FONT>
</P>
<BR>

<P><FONT SIZE=3D2>| [ngfx-io] member Click here to reveal e-mail address =3D YOUR =
ID</FONT>
<BR><FONT SIZE=3D2>| <A =
HREF=3D"http://www.aspfriends.com/aspfriends/ngfx-io.asp"" target="_blank">http://www.aspfriends.com/aspfriends/ngfx-io.asp"; =
TARGET=3D"_blank">http://www.aspfriends.com/aspfriends/ngfx-io.asp</A> =
=3D JOIN/QUIT</FONT>
</P>

</BODY>
</HTML>

-------------------------------------------------------------
This email and any files transmitted are
confidential and intended solely for the
use of the individual or entity to which
they are addressed, whose privacy
should be respected. Any views or
opinions are solely those of the author
and do not necessarily represent those
of the Trencor Group, or any of its
representatives, unless specifically
stated.

Email transmission cannot be guaranteed
to be secure, error free or without virus
contamination. The sender therefore
accepts no liability for any errors or
omissions in the contents of this message,
nor for any virus infection that might result
from opening this message. Trencor is not
responsible in the event of any third party
interception of this email.

If you have received this email in error please notify
Click here to reveal e-mail address For more information about
Trencor, visit www.trencor.net <http://www.trencor.net>

Reply to this message...
 
    
Mitch Denny (VIP)
Carlos,

What you are basically describing is file associations
which are handled automatically by the OS, if you want
to play in that space you need to lookup the HKEY_CLASSES_ROOT
hive in the registry. To work with the registry using
.NET you need to use the RegistryKey (et. al) class
which is contained in the Microsoft.Win32 namespace.

The process goes something like this (I have never
needed to do it before):

    1. Open HKEY_CLASSES_ROOT hive and pull out the
     key for the extension you require, for example
     ".gif".

    2. Take the default REG_SZ value for that key and
     open the key in the HKEY_CLASSES_ROOT hive that
     has that value. On my system that is
"CorelPhotoPaint.Image.10",
     but on yours it will almost certainly be different.

    3. Open the shell|Open|command sub-key of the key
     mentioned above and grab its default value (another
     REG_SZ). Normally, this is the command that you need
     to execute, you replace the %1 in the string with
     the filename that you want the program to load.

Phew! What a pain in the butt hey!?! Anyway, the reason I say
that I have never done this is because it is almost always
easier to use the Process class to kick off the program which
if you use the shell to execute it will do all the above for
you in less than 10 lines of code. For example:

    // Declare locals.
    Process process = null;
    
    // Setup the process.
    process = new Process();
    process.StartInfo = new ProcessStartInfo("photo.gif");
    process.Start();

Thats alot simpler don't you think? Now, I may be wrong, but
aren't you doing all your work server side in an ASP.NET
application? If so, you probably aren't going to want to
kick off the viewer for the document anyway, rather just
let the client download it and let it handle how it opens it.

----------------------------------------
- Mitch Denny
- Click here to reveal e-mail address
- +61 (414) 610-141
-

-----Original Message-----
From: Carlos Magalhaes [mailto:Click here to reveal e-mail address]
Sent: Friday, 7 June 2002 16:35
To: ngfx-io
Subject: [ngfx-io] RE: Strinp the name

Ahh great stuff that will help ! Thanks Jon! Is there no way of taking
that extension and finding out what the relevant program is for that
extension so one can display it e.g. if the extension is .doc then
display word if you cant find the program associated with the extension
the display no program found....

Any ideas?
-----Original Message-----
From: Mitch Denny [mailto:Click here to reveal e-mail address]
Sent: Friday, June 07, 2002 4:06 AM
To: ngfx-io
Subject: [ngfx-io] RE: Strinp the name
Jon,
Thanks! I am glad you posted, I didn't know about the
Path class, just goes to show its difficult to be
across the whole framework :)
----------------------------------------
- Mitch Denny
- Click here to reveal e-mail address
- +61 (414) 610-141
-
-----Original Message-----
From: Jon Gilkison [mailto:Click here to reveal e-mail address]
Sent: Friday, 7 June 2002 11:36
To: ngfx-io
Subject: [ngfx-io] RE: Strinp the name

[Original message clipped]

string extension=Path.Extension(filename);
string filename=Path.GetFileNameWithoutExtension(filename);
Jon Gilkison
//interfacelab.com/

| [ngfx-io] member Click here to reveal e-mail address = YOUR ID
| http://www.aspfriends.com/aspfriends/ngfx-io.asp = JOIN/QUIT

| [ngfx-io] member Click here to reveal e-mail address = YOUR ID
| http://www.aspfriends.com/aspfriends/ngfx-io.asp = JOIN/QUIT
| [ngfx-io] member Click here to reveal e-mail address = YOUR ID |
http://www.aspfriends.com/aspfriends/ngfx-io.asp = JOIN/QUIT
------------------------------------------------------------- This email
and any files transmitted are confidential and intended solely for the
use of the individual or entity to which they are addressed, whose
privacy should be respected. Any views or opinions are solely those of
the author and do not necessarily represent those of the Trencor Group,
or any of its representatives, unless specifically stated. Email
transmission cannot be guaranteed to be secure, error free or without
virus contamination. The sender therefore accepts no liability for any
errors or omissions in the contents of this message, nor for any virus
infection that might result from opening this message. Trencor is not
responsible in the event of any third party interception of this email.
If you have received this email in error please notify
Click here to reveal e-mail address For more information about Trencor, visit
www.trencor.net

Reply to this message...
 
 
System.Diagnostics.Process
System.Diagnostics.ProcessStartInfo
System.IO.FileInfo
System.IO.Path




Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
 
 Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us - Terms of Use - Privacy Policy - www.dotnet247.com