.Split a directory string

M

merrittr

I have a string "c:\upload\file.txt"
what I want is the file name to do this I was trying split on \ but
I cant seem to get it to work?


Server Error in '/appform' Application.
--------------------------------------------------------------------------------

Compilation Error
Description: An error occurred during the compilation of a resource
required to service this request. Please review the following specific
error details and modify your source code appropriately.

Compiler Error Message: CS1502: The best overloaded method match for
'string.Split(params char[])' has some invalid arguments

Source Error:



Line 234: string strFileNameOnServer = txtServername.Value;
Line 235: string sep = @"\";
Line 236: Array a = strFileNameOnServer.Split(sep);
 
C

Christopher Reed

Try using this:

string strFileNameOnServer = txtServername.Value;
char[] sep = {'\\'};
string[] a = strFileNameOnServer.Split(sep);
 
M

Michael Nemtsev

Hello merrittr,

Why not to use
string fileName = @"C:\mydir\myfile.ext";
string path = @"C:\mydir\";
string result;
result = Path.GetFileName(fileName);

instead of splitting ?

m> I have a string "c:\upload\file.txt"
m> what I want is the file name to do this I was trying split on \ but
m> I cant seem to get it to work?

---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/members/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsch
 
C

Cletus Van Damme

Christopher Reed said:
Try using this:

string strFileNameOnServer = txtServername.Value;
char[] sep = {'\\'};
string[] a = strFileNameOnServer.Split(sep);

--
Christopher A. Reed
"The oxen are slow, but the earth is patient."

merrittr said:
I have a string "c:\upload\file.txt"
what I want is the file name to do this I was trying split on \ but
I cant seem to get it to work?


Server Error in '/appform' Application.
--------------------------------------------------------------------------------

Compilation Error
Description: An error occurred during the compilation of a resource
required to service this request. Please review the following specific
error details and modify your source code appropriately.

Compiler Error Message: CS1502: The best overloaded method match for
'string.Split(params char[])' has some invalid arguments

Source Error:



Line 234: string strFileNameOnServer = txtServername.Value;
Line 235: string sep = @"\";
Line 236: Array a = strFileNameOnServer.Split(sep);

I prefer to let the framework do the "heavy lifting". Check out the
System.IO.Path class:

using System;
using System.Diagnostics;
using System.IO;

void SomeFunction()
{
string filePath = @"C:\SomeFolder\file.txt";
string fileName = Path.GetFileName(filePath);
Debug.WriteLine(fileName); // prints "file.txt"
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top