.Split a directory string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • merrittr

    #1

    .Split a directory string

    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(p arams char[])' has some invalid arguments

    Source Error:



    Line 234: string strFileNameOnSe rver = txtServername.V alue;
    Line 235: string sep = @"\";
    Line 236: Array a = strFileNameOnSe rver.Split(sep) ;

  • Christopher Reed

    #2
    Re: .Split a directory string

    Try using this:

    string strFileNameOnSe rver = txtServername.V alue;
    char[] sep = {'\\'};
    string[] a = strFileNameOnSe rver.Split(sep) ;

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

    "merrittr" <merrittr@gmail .com> wrote in message
    news:1135747847 .877887.297520@ g49g2000cwa.goo glegroups.com.. .[color=blue]
    >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(p arams char[])' has some invalid arguments
    >
    > Source Error:
    >
    >
    >
    > Line 234: string strFileNameOnSe rver = txtServername.V alue;
    > Line 235: string sep = @"\";
    > Line 236: Array a = strFileNameOnSe rver.Split(sep) ;
    >[/color]


    Comment

    • Michael Nemtsev

      #3
      Re: .Split a directory string

      Hello merrittr,

      Why not to use
      string fileName = @"C:\mydir\myfi le.ext";
      string path = @"C:\mydir\" ;
      string result;
      result = Path.GetFileNam e(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 Nietzsche


      Comment

      • Cletus Van Damme

        #4
        Re: .Split a directory string


        "Christophe r Reed" <carttu@nospam. nospam> wrote in message
        news:O7BMLN3CGH A.3876@tk2msftn gp13.phx.gbl...[color=blue]
        > Try using this:
        >
        > string strFileNameOnSe rver = txtServername.V alue;
        > char[] sep = {'\\'};
        > string[] a = strFileNameOnSe rver.Split(sep) ;
        >
        > --
        > Christopher A. Reed
        > "The oxen are slow, but the earth is patient."
        >
        > "merrittr" <merrittr@gmail .com> wrote in message
        > news:1135747847 .877887.297520@ g49g2000cwa.goo glegroups.com.. .[color=green]
        >>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(p arams char[])' has some invalid arguments
        >>
        >> Source Error:
        >>
        >>
        >>
        >> Line 234: string strFileNameOnSe rver = txtServername.V alue;
        >> Line 235: string sep = @"\";
        >> Line 236: Array a = strFileNameOnSe rver.Split(sep) ;
        >>[/color]
        >
        >[/color]

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

        using System;
        using System.Diagnost ics;
        using System.IO;

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


        Comment

        • merrittr

          #5
          Re: .Split a directory string

          whoaaa that was fast
          this works perfectly thanks for the help guys

          Comment

          Working...