hi there,
i have a code that try to get the file name from the path the file is the code is below

a = @"C:\Documents and Settings\awaduge\Desktop\Purchase Order\Purchase Order Excel\20100427 REQ (ASG NI Framegrabber for ARL Camera).xls";
                    MessageBox.Show(a.ToString());

                    int Findex = a.LastIndexOf('\\');
                    MessageBox.Show("index of a " + Findex.ToString());

                    int Lindex = a.IndexOf(".");
                    MessageBox.Show("index of s " + Lindex.ToString());

                    MessageBox.Show("length " + a.Length.ToString());

                    FileName = a.Substring(Findex,Lindex);
                    MessageBox.Show(FileName);

i want to get the substring "20100427 REQ (ASG NI Framegrabber for ARL Camera)" from the string a defined in the code,

Findex is 77 and Lindex is 127,but actually the index of the last \ is 77. and also when i try to get the file name it gives an exception saying argument out of range exception. but it is not out of range i i complied the code line by line,

why is this coming,
how can i get the file name from the string a ???

thanxxx

Use System.IO.Path.GetFileNameWithoutExtension

string fullfilename = @"C:\filename.txt";
string filenameonly = System.IO.Path.GetFileNameWithoutExtension(fullfilename);
// filenameonly is now "filename"

Use System.IO.Path.GetFileNameWithoutExtension

string fullfilename = @"C:\filename.txt";
string filenameonly = System.IO.Path.GetFileNameWithoutExtension(fullfilename);
// filenameonly is now "filename"

hey i got the solution
in the sudstring method you have to use the index number as the first parameter and the second index is the number of character that is going to be displayed.

thnakxxxxxx

Glad you could figure it out on your own. You are more likely to remember for next time.
I still think my way is better for getting the filename from a path.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.