Can anybody please tell me how to find the location where my data and log files stored using SMO in c#?
Asked
Active
Viewed 4,043 times
3 Answers
4
public static void foo() {
Microsoft.SqlServer.Management.Smo.Server server = new ServerConnection("<server name>");
Microsoft.SqlServer.Management.Smo.Database db = server.Databases["<database name>"];
Console.WriteLine(db.FileGroups[0].Files[0].FileName);
Console.WriteLine(db.LogFiles[0].FileName);
}
This example assumes you have sufficient rights to the Server\Database, and only returns the full path\filename for the first db/log file in the filegroup.
FileGroups, Files, and LogFiles are SMO collections that will contain one or more of it's respective items.

C-Pound Guru
- 15,967
- 6
- 46
- 67
0
$targetServerName = "localhost"
$targetDatabaseName = "dbname"
$targetServer = New-Object ("Microsoft.SqlServer.Management.Smo.Server")$targetServerName
$database = $targetServer.Databases[$targetDatabaseName]
foreach ($fg in $database.FileGroups)
{
foreach ($df in $fg.Files)
{
"Filegroup type : " + $fg.Name + " DataFiles : " + $df.FileName
}
}
foreach ($lf in $database.LogFiles)
{
"Log file : " + $lf.FileName
}
That should get you the exact paths.

Mouffette
- 732
- 1
- 7
- 19