i have the following problem: i have a sql table ("pathtable") in which i save the paths to my archived xml files in a column called "path". i now want to extract the filename ("filename") from said file ("xml1.xml") in the path from the column i get when i use the query i mention in my code (SELECT path FROM pathtable).
in other words, let's say there's a file called "xml1.xml" in the folder xmlfiles (xmlfiles). and the path (C:\Users\xmlfiles) to said folder is saved in my sql table in the column "path".
In a perfect world the result of my code would be just the name of the file: "xml1" (without the file extension)
Thanks for your help, regards Brian
SqlConnection connection = new SqlConnection(@"Data Source=...\SQLEXPRESS;Initial Catalog=...;Integrated Security=SSPI");
{
string pathname= "SELECT path FROM pathtable";
SqlCommand cmd = new SqlCommand(pathname, connection);
connection.Open();
SqlDataReader reader = cmd.ExecuteReader();
string strPath = pathname; //"C:\Users\xmlfiles\";
string filename = null;
filename = Path.GetFileName(strPath);
while (reader.Read())
{
Console.Write(filename);
}
Console.ReadKey();
reader.Close();
connection.Close();
}
because there seems to be a lot of confusion on what i want to achieve (my bad :)), i try to explain it in a better way: this is my sql table: SQL Table
and in this path: "C:\Users\xmlfiles\" there is a file called "testxml.xml" and i want my code to return "testxml". and one more requirement is that it's hard-coded, in other words i can only change the path i want to use in my sql table, not in the C# code. so let's say i update my sql table from "C:\Users\xmlfiles\" to "C:\Users\docfiles\" then it should give me the filename from the folder docfiles.
i'm just getting started, so thanks for your patience
whileloop likestring path = reader.GetString(0);and the file names without extension likestring file = Path.GetFileNameWithoutExtension(path);