Find all Folders with Certain Size Parameters? - recursion

I am looking for a good way to find all folders that have contents that are say 500k or less. I think this involves recursively going to the lowest folder in a hierarchy and then "reading up" to get a size total. All ideas appreciated (something int he .NET framework or PowerShell if I really have to), thanks.

This could help you out.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace GetDirectorySize
{
class Program
{
static void Main(string[] args)
{
long maxFolderSizeInBytes = 20000000;
foreach (var directory in Directory.GetDirectories(#"C:\Projects\Visual Studio 2010\"))
{
string[] a = Directory.GetFiles(directory, "*.*");
long i = 0;
foreach (string name in a)
{
FileInfo info = new FileInfo(name);
i += info.Length;
}
if (i <= maxFolderSizeInBytes)
{
Console.WriteLine(directory);
}
}
Console.ReadLine();
}
}
}

Related

how can i reduce memory usage on my application running on azure

I am learning how to use the .NET framework. I am working with ASP .NET core. I have never had or hit my azure webhosting quota until recently I keep hitting quota by making very few request and this started ever since I installed dotnetbrowser library. its the best library for my project because it makes getting data easier. however, I will appreciate if someone can tell me how to get same data without using a browser control like web browser or dotnetbrowser. the data I needed go through multiple server and client communications before the needed value is provided. So my question is how can achieve the same thing without using browser control?
finally, my code might be buggy given that I am not too familiar with threads and task. I might be using too much memory. so below is my code
using DotNetBrowser;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Web;
using System.Web.Http;
namespace AjaxRequest.Controllers
{
public class ValuesController : ApiController
{
private static ManualResetEvent waitEvent;
private static List<string> ajaxUrls = new List<string>();
static string str = "";
public static Browser browser;
public ValuesController()
{
waitEvent = new ManualResetEvent(false);
browser = BrowserFactory.Create();
browser.Context.NetworkService.ResourceHandler = new AjaxResourceHandler();
browser.Context.NetworkService.NetworkDelegate = new AjaxNetworkDelegate();
}
// GET api/values
public string Get(int id, string title)
{
string Title = title.Replace(" ", "-");
browser.LoadURL(string.Format("https://ba.com/foo/{0}-{1}/something.html", Title, id));
waitEvent.WaitOne();
browser.Dispose();
string Json = Regex.Replace(str, #"\\","");
return Json.Replace("\\\"", "\"");
}
public class AjaxResourceHandler : ResourceHandler
{
//HomeController hc;
public bool CanLoadResource(ResourceParams parameters)
{
if (parameters.ResourceType == ResourceType.XHR && parameters.URL.Contains("https://something.com/ajax/blahblah"))
{
ajaxUrls.Add(parameters.URL);
}
return true;
}
}
public class AjaxNetworkDelegate : DefaultNetworkDelegate
{
//HomeController hc;
public override void OnDataReceived(DataReceivedParams parameters)
{
if (ajaxUrls.Contains(parameters.Url))
{
PrintResponseData(parameters.Data);
}
}
public void PrintResponseData(byte[] data)
{
str = Encoding.UTF8.GetString(data);
ajaxUrls.Clear();
browser.Stop();
browser.dispose();
waitEvent.Set();
}
public void error(string info)
{
str = info;
waitEvent.Set();
}
}
}
}
is it possible that I am doing it wrong? if that's the case how can it be improved to conserve memory or data?
UPDATE: am using azure free hosting services
DotNetBrowser is a Chromium wrapper - I am not entirely sure why you would need it in a web app, but that said, it is likely it is the culprit. Once you remove it, you can use HttpClient to perform the right requests with no memory overhead.
Profiling-wise, your best bet is to start with Application Insights - it's enabled by default in ASP.NET Core projects. It will allow resource tracking across app components.
It seems like you have more than one running Browser instance.
I can suggest to check that Browser instance is disposed correctly. If not, you can try to dispose it in the Dispose method of the controller.

Retry for three times If the File Exists in c#

My Requirement is i want to retry if the specific file exists,suppose like currently i'm doing svn installation / uninstallation so if the svn is installed then it will create RevisionNumber.txt and if the svn is uninstalled then ReveisionNumber.txt is deleted
So My Requirement is if RevisionNumber.txt exists / Svn exists i want to retry the process of svn uninstallation for 3 times.
can anyone please sort me out ? below is my code :
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace SVNUNINSTALL
{
class Program
{
public void Process()
{
System.Diagnostics.Process.Start("C:\\Users\\sk185462\\Desktop\\SVNUPDATED\\SvnUninstallation.exe");
bool exists = File.Exists("C:\\Users\\sk185462\\Desktop\\SVNUPDATED\\RevisionNumber.txt");
Console.WriteLine(exists);
Console.ReadLine();
}
static void main(String[] args)
{
ProcessStartInfo startInfo = new ProcessStartInfo();
}
}
}
public void Process()
{
bool exists = File.Exists("C:\\Users\\sk185462\\Desktop\\SVNUPDATED\\RevisionNumber.txt");
for(int i = 0; i < 3 && exists; i++)
{
System.Diagnostics.Process.Start("C:\\Users\\sk185462\\Desktop\\SVNUPDATED\\SvnUninstallation.exe");
Sleep(2000); // or long enough to ensure the uninstall process finishes executing
exists = File.Exists("C:\\Users\\sk185462\\Desktop\\SVNUPDATED\\RevisionNumber.txt");
}
Console.WriteLine(exists);
Console.ReadLine();
}

Unable to create component using Coreservice

I am trying to create component using coreservice using the below code, and when i execute the exe, am getting the error "unable to find the uuid:""64c7e56a-161d-4698-a76b-7fd96227948d:Content".
I have opened the schema which am linking to this component, and i seen this UUID over there also.
as of now am just trying to create a component by providing the folder, schema, title as hard corded.
if you can guide me how to add field value also in component, that would be great. (For example assume that i have a field "Text" in my schema which am linking to this component, and i want to add "This is the Text" in this field of my component using the same program.
can you please help me out on this?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DctmToSDLMigration.SDLCoreServiceReference;
namespace DctmToSDLMigration
{
class Program
{
static DctmToSDLMigration.SDLCoreServiceReference.SessionAwareCoreService2010Client client = new SessionAwareCoreService2010Client();
static ReadOptions readoptions = new ReadOptions();
static void CreateComponent()
{
try
{
string TargetFolderTcmId = "tcm:148-1263-2";
string LinkSchemaTcmId = "tcm:148-11460-8";
ComponentData CurrentMigrationComponent = client.GetDefaultData(ItemType.Component, TargetFolderTcmId) as ComponentData;
LinkToSchemaData SchemaToUse = new LinkToSchemaData();
SchemaToUse.IdRef = LinkSchemaTcmId.ToString();
CurrentMigrationComponent.Schema = SchemaToUse ;
CurrentMigrationComponent.Title = "Test component";
client.Create(CurrentMigrationComponent, readoptions);
Console.WriteLine(CurrentMigrationComponent.Id);
Console.ReadLine();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.ReadLine();
}
}
static void Main(string[] args)
{
CreateComponent();
}
}
}
You need to set the Content Property of the Component.
XmlDocument doc = new XmlDocument();
doc.LoadXml(string.Format(#"<Content xmlns='{0}'><Test>Hello</Test></Content>", SchemaToUse.NamespaceUri));
CurrentMigrationComponent.Content = doc.DocumentElement;

How to create and run custom tasks in Telligent 5.5

Does anyone know any resources regarding to the creation of a custom scheduled task under Telligent 5.5 ?
From what I read, all I need to do is the following:
1.Create a type that implements the ITask2 interface
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Telligent.Tasks;
namespace Project.ScheduledTasks
{
public class ReminderTask:ITask2
{
public void Execute()
{
string task = "Please hit the breakpoint here";
}
public void Load(System.Xml.XmlNode node)
{
throw new NotImplementedException();
}
}
}
2.Add the task description in the communityserver.config
<Thread minutes="1">
<task name="ReminderTask" type="Project.ScheduledTasks.ReminderTask, Project.ScheduledTasks" enabled="true" enableShutDown="false"></task>
</Thread>
Do I need to do anything else ?
Please help :).
Yes, that is all that you should need to do. Are you having issues?

Extract text and image from a pdf in a selected area or coordinates

I have a specific requirement of extracting text and images from a specific area in a pdf file.The area might be a selected or highlighted or from a given set of coordinates.
When i went through, all the approaches are to extract images and text entirely from the PDF on not in a specified location.
I tried with iTextSharp,Syncfussion,Apose but couldn figure out a better approach for this.
If somebody could help me out in this it would be greatfull. Can you share your ideas and suggestion on how to implement this in .net.
Regards,
Arun.M
this code extract images from pdf
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Drawing.Imaging;
using System.IO;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Bytescout.PDFExtractor;
namespace ExtractAllImages
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// This test file will be copied to the project directory on the pre-build event (see the project properties).
String inputFile = Server.MapPath("sample1.pdf");
// Create Bytescout.PDFExtractor.ImageExtractor instance
ImageExtractor extractor = new ImageExtractor();
extractor.RegistrationName = "demo";
extractor.RegistrationKey = "demo";
// Load sample PDF document
extractor.LoadDocumentFromFile("sample1.pdf");
Response.Clear();
int i = 0;
// Initialize image enumeration
if (extractor.GetFirstImage())
{
do
{
if (i == 0) // Write the fist image to the Response stream
{
string imageFileName = "image" + i + ".png";
Response.Write("<b>" + imageFileName + "</b>");
Response.ContentType = "image/png";
Response.AddHeader("Content-Disposition", "inline;filename=" + imageFileName);
// Write the image bytes into the Response output stream
Response.BinaryWrite(extractor.GetCurrentImageAsArrayOfBytes());
}
i++;
} while (extractor.GetNextImage()); // Advance image enumeration
}
Response.End();
}
}
}

Resources