ServerManager How to get site's physical path on disk? - c#

How I can get physical path of site on a disk ?
using (ServerManager serverManager = new ServerManager()) {
var sites = serverManager.Sites;
foreach (Site site in sites) {
Console.WriteLine(site.Name); // This will return the WebSite name
//but how i get it's path on disc ??????
}

ServerManager m = new ServerManager();
m.Sites["default web site"].Applications["/"].VirtualDirectories["/"].PhysicalPath;
basically every site has a "root application" which must have a "root virtual directory".

To improve a bit what others wrote, remember if you have multiple application pools pr site you get multiple directories.
using (var iisManager = new ServerManager())
{
SiteCollection sites = iisManager.Sites;
foreach (var site in sites)
{
Console.WriteLine(site.Name);
foreach(var app in site.Applications)
{
foreach (var virt in app.VirtualDirectories)
{
Console.WriteLine(site.Name);
}
}
}
}

Use the following code to get path
using (ServerManager serverManager = new ServerManager())
{
var sites = serverManager.Sites;
foreach (Site site in sites)
{
foreach (Application app in site.Applications)
{
Console.WriteLine("path: {0}", app.Path);
}
}
}

Related

How to get URLs opened in all the browsers running on PC using C#?

I tried below code but it returns only the title (i.e not the url) of currently active tab on browser.
public void TrackUrls()
{
List<string> browsers = new List<string>() { "Google Chrome", "Microsoft Edge" };
foreach (var item in browsers)
{
Process[] arrprocesses = Process.GetProcessesByName(item);
foreach (Process pro in arrprocesses)
{
if (pro.MainWindowTitle.Length != 0)
File.AppendAllText(appDataPath + #"\Sleep\\sleep.text", " "+pro.MainWindowTitle + "\r\n")//here we are saving data in file;
}
}
}

ServerManager iis check default site wasnt change c#

I need to overwritte Default Site if isnt use, overwrite it if the user decides so or create a new one.
I am trying check all sites, and take this with port 80 and name "Default Web Site". Next if it exist I want to take physical path and check does it equal the default path and contains all files for default path. In this way:
private static void CheckSite(SiteCollection applicationSites)
{
int i = 0;
foreach (var item in applicationSites)
{
foreach (var binding in item.Bindings)
{
int port;
if (binding.EndPoint != null)
port = binding.EndPoint.Port;
else
continue;
if (port == 80 && item.Name == "Default Web Site")
{
var path = item.Applications[i].VirtualDirectories[0].PhysicalPath;
string[] dire = Directory.GetFiles(path);
}
}
i++;
}
}
Is there better way to do this? I am using Microsoft.Web.Administration.
You can get the Default Web Site as below code:
using (ServerManager serverManager = new ServerManager())
{
var sites = serverManager.Sites;
foreach (Site site in sites)
{
var sitename = site.Name;
if (sitename == "Default Web Site")
{
}
}
}

C# sharepoint loop through all files in folder and all subfolders

I am trying to loop after all files in a folder and all subfolders as deep as it goes from the first folder.
I have found a way but I think it's stupid and is probably a way better method to do it.
The code loops through the first folder and all files. After that it loops again the subfolders then files and again for the third time.
Is there some other way I can do this? just choose one folder then it loops down the hierarchy automatically.
static void ReadAllSubs(string siteUrl, string siteFolderPath, string localTempLocation)
{
ClientContext ctx = new ClientContext(siteUrl);
ctx.AuthenticationMode = ClientAuthenticationMode.Default;
SecureString passWord = new SecureString();
string pwd = "xxx";
foreach (char c in pwd.ToCharArray()) passWord.AppendChar(c);
ctx.Credentials = new SharePointOnlineCredentials("test#test.com", passWord);
FolderCollection folderCollection = ctx.Web.GetFolderByServerRelativeUrl("Delte%20dokumenter/07 - Detaljprosjekt").Folders;
// Don't just load the folder collection, but the property on each folder too
ctx.Load(folderCollection, fs => fs.Include(f => f.ListItemAllFields));
// Actually fetch the data
ctx.ExecuteQuery();
foreach (Folder folder in folderCollection)
{
//LOOP MAIN FOLDER
Console.WriteLine("---------------FIRST LAYER FOLDER---------------------");
var item = folder.ListItemAllFields;
var folderpath = item["FileRef"];
FolderCollection LoopFolder = ctx.Web.GetFolderByServerRelativeUrl(folderpath.ToString()).Folders;
ctx.Load(LoopFolder, fs => fs.Include(f => f.ListItemAllFields));
ctx.ExecuteQuery();
Console.WriteLine(folderpath);
//LOOP ALL FILES IN FIRST MAIN FOLDER
FileCollection mainfiles = ctx.Web.GetFolderByServerRelativeUrl(folderpath.ToString()).Files;
ctx.Load(mainfiles);
ctx.ExecuteQuery();
Console.WriteLine("---------------FIRST LAYER FILES---------------------");
foreach (File mainfile in mainfiles)
{
Console.WriteLine(mainfile.Name);
Console.WriteLine(mainfile.MajorVersion);
}
//LOOP SUBFOLDER
Console.WriteLine("---------------SECOUND LAYER FOLDER---------------------");
foreach (Folder ff in LoopFolder)
{
var subitem = ff.ListItemAllFields;
var folderpathsub = subitem["FileRef"];
Console.WriteLine(folderpathsub);
//LOOP ALL FILES IN FIRST SUBFOLDER
FileCollection files = ctx.Web.GetFolderByServerRelativeUrl(folderpathsub.ToString()).Files;
ctx.Load(files);
ctx.ExecuteQuery();
Console.WriteLine("---------------SECOUND LAYER FILES---------------------");
foreach (File file in files)
{
Console.WriteLine(file.Name);
Console.WriteLine(file.MajorVersion);
}
var created = (DateTime)item["Created"];
var modified = (DateTime)item["Modified"];
Console.WriteLine("---------------THIRD LAYER FOLDER---------------------");
FolderCollection ThirdLoopFolder = ctx.Web.GetFolderByServerRelativeUrl(folderpathsub.ToString()).Folders;
ctx.Load(ThirdLoopFolder, fs => fs.Include(f => f.ListItemAllFields));
ctx.ExecuteQuery();
foreach (Folder fff in ThirdLoopFolder)
{
var item3 = fff.ListItemAllFields;
var folderpath3 = item3["FileRef"];
Console.WriteLine(folderpath3);
//LOOP ALL FILES IN THIRD SUBFOLDER
FileCollection thirdfiles = ctx.Web.GetFolderByServerRelativeUrl(folderpath3.ToString()).Files;
ctx.Load(thirdfiles);
ctx.ExecuteQuery();
Console.WriteLine("---------------THIRD LAYER FILES---------------------");
foreach (File file in thirdfiles)
{
Console.WriteLine(file.Name);
Console.WriteLine(file.MajorVersion);
}
}
}
}
}
I may propose two solutions.
First method
The first would be a recursive approach similar to Your solution.
private static void UseRecursiveMethodToGetAllItems()
{
using (var context = new ClientContext(WebUrl))
{
context.Credentials = new SharePointOnlineCredentials(UserName, Password);
var rootFolders = context.Web.GetFolderByServerRelativeUrl(LibName).Folders;
context.Load(rootFolders, folders => folders.Include(f => f.ListItemAllFields));
context.ExecuteQuery();
foreach (var folder in rootFolders)
{
GetFilesAndFolders(context, folder);
}
Console.ReadLine();
}
}
private static void GetFilesAndFolders(ClientContext context, Folder folder)
{
if (folder != null && folder.ListItemAllFields.FieldValues.Count > 0)
{
Console.WriteLine($"Folder - {folder.ListItemAllFields.FieldValues["FileLeafRef"]}");
var fileCollection = folder.Files;
context.Load(fileCollection, files => files.Include(f => f.ListItemAllFields));
context.ExecuteQuery();
foreach(var file in fileCollection)
{
Console.WriteLine($" -> {file.ListItemAllFields.FieldValues["FileLeafRef"]}");
}
var subFolderCollection = folder.Folders;
context.Load(subFolderCollection, folders => folders.Include(f => f.ListItemAllFields));
context.ExecuteQuery();
foreach (var subFolder in subFolderCollection)
{
GetFilesAndFolders(context, subFolder);
}
}
}
the first function does the authentication to the given WebUrl and gets the folders from the root folder (which is the name of the library). Then the second method is recursive. First gets all files from the current folder and prints them to the console, after that the next step is to query the subfolders in this folder and then do the same method.
I have created a sample library with folders and files and the result of the above method is
Second method
The second method is a bit more 'flat'. It is possible to create a CAML query to get all items from library recursively and then check if it's file or folder. All items have path property to determine the hierarchy.
private static void UseQueryToGetAllItems()
{
using (var context = new ClientContext(WebUrl))
{
context.Credentials = new SharePointOnlineCredentials(UserName, Password);
List<ListItem> result = new List<ListItem>();
try
{
ListItemCollectionPosition position = null;
int page = 1;
do
{
List list = context.Web.Lists.GetByTitle(LibName);
CamlQuery query = new CamlQuery();
query.ViewXml = new StringBuilder()
.Append("<View Scope=\"RecursiveAll\">")
.Append("<Query>")
.Append("")
.Append("</Query>")
.Append("<RowLimit>5000</RowLimit>")
.Append("</View>")
.ToString();
query.ListItemCollectionPosition = position;
ListItemCollection items = list.GetItems(query);
context.Load(items);
context.ExecuteQuery();
position = items.ListItemCollectionPosition;
if (items.Count > 0)
result.AddRange(items);
context.ExecuteQuery();
page++;
}
while (position != null);
result.ForEach(item =>
{
Console.WriteLine($"{item["ID"]}) Path: {item["FileDirRef"]} - Name: {item["FileLeafRef"]} - Type: {item.FileSystemObjectType}");
});
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
Console.ReadLine();
}
}
This method also does the authentication to the same library and then executes a query to get all the items to the list (the Query is done in a pagination way to overcome the threshold limit to get more than 5000 elements in query). After the method gets the list of all items it prints them out presenting the path, name of the file, and type (file or folder.. or other.. If remember well there might be also web and some other in this enum).
For the same library as the first method The result of this approach is
hope it helps :)

How can I download files from Sharepoint?

I'm browsing my instance, https://instance.sharepoint.com/1234/abc
That page contains a list of several folders and files. How do I download files from that path?
ClientContext cxt = new ClientContext(fullWebUrl);
cxt.Credentials = new SharePointOnlineCredentials(username, new NetworkCredential("", password).SecurePassword);
List list = cxt.Web.Lists.GetByTitle("Documents");
cxt.Load(list);
cxt.ExecuteQuery();
FolderCollection fcol = list.RootFolder.Folders;
List<string> lstFile = new List<string>();
foreach (Folder f in fcol)
{
if (f.Name == "filename")
{
cxt.Load(f.Files);
cxt.ExecuteQuery();
FileCollection fileCol = f.Files;
foreach (Microsoft.SharePoint.Client.File file in fileCol)
{
lstFile.Add(file.Name);
}
}
}
This fails down at the foreach with the error
Microsoft.SharePoint.Client.CollectionNotInitializedException: 'The collection has not been initialized
Would not cxt.ExecuteQuery do the job?
you can make a mass download from a SharePoint library without using code, just do the following:
1:go to your SharePoint Library
2:copy the full path
for example
https://sharepointdomain.com/sites/yoursite/yourLibraryName/forms/Allitems.aspx
now remove
/forms/allitems.aspx from the address and you will remain with
https://sharepointdomain.com/sites/yoursite/yourLibraryName
copy that and open your windows file browser and paste that in the address bar and your library will be opened as a local folder, then you can select all files and copy them to any destination even you can move them to another SharePoint library you might be prompted to enter your SharePoint credentials make sure to enter it is as follow:
username is : domain\your SharePoint user name
password : your SharePoint login password.
You need to load the FolderCollection fcol first (cxt.Load(fcol)):
List list = cxt.Web.Lists.GetByTitle("Documents");
cxt.Load(list);
cxt.ExecuteQuery();
FolderCollection fcol = list.RootFolder.Folders;
cxt.Load(fcol);
cxt.ExecuteQuery();
List<string> lstFile = new List<string>();
foreach (Folder f in fcol)
{
if (f.Name == "filename")
{
cxt.Load(f.Files);
cxt.ExecuteQuery();
FileCollection fileCol = f.Files;
foreach (Microsoft.SharePoint.Client.File file in fileCol)
{
lstFile.Add(file.Name);
}
}
}
To download the file:
foreach (Microsoft.SharePoint.Client.File file in fileCol)
{
var localstream = System.IO.File.Open("c:/" + file.Name, System.IO.FileMode.CreateNew);
var fileInfo = File.OpenBinaryDirect(cxt, file.ServerRelativeUrl);
var spstream = fileInfo.Stream;
spstream.CopyTo(localstream);
}

how to display all folder and sub folder from FTP using c# windows application?

i want to display the all the files and sub folders from FTP using c#
how do i do?
can you one upload code?
i have tried import data from FTP in Tree view.but i cannot do
my code:
private TreeNode CreateDirectoryNode(string root, string p)
{
var directoryNode = new TreeNode("CGT");
var directoryListing = GetDirectoryListing(path);
var directories = directoryListing.Where(d => d.IsDirectory);
var files = directoryListing.Where(d => !d.IsDirectory);
foreach (var dir in directories)
{
directoryNode.Nodes.Add(CreateDirectoryNode(dir.FullPath, dir.Name));
}
foreach (var file in files)
{
directoryNode.Nodes.Add(new TreeNode(file.Name));
}
return directoryNode;
}
Leave this code this for example only..you people please help me out of here

Categories

Resources