I am writing a WPF application for disk analisis.
I wrote this function to calculate the size of the folders
static long GetDirectorySize(DirectoryInfo root, bool recursive = true)
{
FileInfo[] files = null;
DirectoryInfo[] subDirs = null;
var startDirectorySize = default(long);
// First, process all the files directly under this folder
try
{
files = root.GetFiles("*.*");
}
// This is thrown if even one of the files requires permissions greater
// than the application provides.
catch (UnauthorizedAccessException)
{
}
catch (DirectoryNotFoundException)
{
}
if (files != null)
{
//Add size of files in the Current Directory to main size.
foreach (var fileInfo in files)
Interlocked.Add(ref startDirectorySize, fileInfo.Length);
}
// Now find all the subdirectories under this directory.
if (recursive)
{ //Loop on Sub Direcotries in the Current Directory and Calculate it's files size.
try
{
subDirs = root.GetDirectories();
Parallel.ForEach(subDirs, (subDirectory) =>
Interlocked.Add(ref startDirectorySize, GetDirectorySize(subDirectory, recursive)));
}
catch (UnauthorizedAccessException)
{
}
catch (DirectoryNotFoundException)
{
}
}
return startDirectorySize;
}
How can I output a current size on kind of label? I understand, that I have to use a dispatcher, but I don't know how to call it every time any thread is changing the current calculated size. Also, I am starting this
private void btnStart_Click(object sender, RoutedEventArgs e)
{
Task.Factory.StartNew(() => this.GetDirectorySize(new DirectoryInfo(#"C:\"), true));
}
Am i right? How should I change my code?
Related
How to read Hadoop sequence file in Flink? I hit multiple issues with the approach below.
I have:
DataSource<String> source = env.readFile(new SequenceFileInputFormat(config), filePath);
and
public static class SequenceFileInputFormat extends FileInputFormat<String> {
...
#Override
public void setFilePath(String filePath) {
org.apache.hadoop.conf.Configuration config = HadoopUtils.getHadoopConfiguration(configuration);
logger.info("Initializing:"+filePath);
org.apache.hadoop.fs.Path hadoopPath = new org.apache.hadoop.fs.Path(filePath);
try {
reader = new SequenceFile.Reader(hadoopPath.getFileSystem(config), hadoopPath, config);
key = (Writable) ReflectionUtils.newInstance(reader.getKeyClass(), config);
value = (Writable) ReflectionUtils.newInstance(reader.getValueClass(), config);
} catch (IOException e) {
logger.error("sequence file creation failed.", e);
}
}
}
One of the issues: Could not read the user code wrapper: SequenceFileInputFormat.
Once you get an InputFormat, you can call ExecutionEnvironment.createInput(<input format>) to create your DataSource.
For SequenceFiles, the type of the data is always Tuple2<key, value>, so you have to use a map function to convert to whatever type you're trying to read.
I use this code to read a SequenceFile that contains Cascading Tuples...
Job job = Job.getInstance();
FileInputFormat.addInputPath(job, new Path(directory));
env.createInput(HadoopInputs.createHadoopInput(new SequenceFileInputFormat<Tuple, Tuple>(), Tuple.class, Tuple.class, job);
I hava c# winforms application and in the form I have a button. On click of the button I want to commit all files in a particular folder to the SVN repository , but i keep getting
Folder is not a working copy error.
Below code is called on click event:
private void Save_Config_Click(object sender, EventArgs e)
{
#if DEBUG
path = #"../../../Application";
#endif
using (SvnClient client = new SvnClient())
{
SvnCommitArgs args = new SvnCommitArgs();
args.LogMessage = "test";
args.ThrowOnError = true;
args.ThrowOnCancel = true;
try
{
client.Commit(path, args);
}
catch (Exception s)
{
if (s.InnerException != null)
{
throw new Exception(s.InnerException.Message, s);
}
throw s;
}
To Commit from a folder, that folder must be a working copy.
You must checkout before you make the commit.
I'm copying a one directory(include all sub directories and files) present in one drive into another drive with same names for that I want to show a Progress bar i have seen so many examples with timers but i dont know how much time that will take so please guide me how to achieve that
I written the code like this
private void btnDriveSubmit_Click(object sender, EventArgs e)
{
ErrorProvider errorproviderDrive = new ErrorProvider();
if (!String.IsNullOrEmpty(tbDrive.Text))
{
if (tbDrive.TextLength == 1)
{
drive = tbDrive.Text;
string org1 = Application.StartupPath + "\\UserFirstDetails.xml";
UsrDetails.Load(org1);
XmlNode drivetag = UsrDetails.SelectSingleNode("UserFirstDetails/Drive");
drivetag.InnerText = drive;
FileInfo fp = new FileInfo(org1);
fp.Attributes = FileAttributes.Normal;
UsrDetails.Save(org1);
DirectoryInfo diSource = new DirectoryInfo(sourcedrive+"Palle_University");
DirectoryInfo diTarget = new DirectoryInfo(tbDrive.Text+":\\Palle_University");
CopyAll(diSource, diTarget);
this.Hide();
}
else
{
errorproviderDrive.SetError(tbDrive, "Length should be one character only");
}
}
else
{
errorproviderDrive.SetError(tbDrive, "Drive Should not be empty");
}
}
public void CopyAll(DirectoryInfo source, DirectoryInfo target)
{
DirectoryInfo di = Directory.CreateDirectory(target.FullName);
// Copy each file into the new directory.
foreach (FileInfo fi in source.GetFiles())
{
Console.WriteLine(#"Copying {0}\{1}", target.FullName, fi.Name);
fi.CopyTo(Path.Combine(target.FullName, fi.Name), true);
}
// Copy each subdirectory using recursion.
foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
{
DirectoryInfo nextTargetSubDir =
target.CreateSubdirectory(diSourceSubDir.Name);
CopyAll(diSourceSubDir, nextTargetSubDir);
}
}
To calculate the progress of files copy operation to display in a progressbar control, you need to get the total number of files in the source directory
First declare two general variables
int totalFilescount; //Total number of files in the source directory
int currentFileindex; //Incremented when a file is copied to the destination directory
Before calling CopyAll, you need to get the total number of files in the source dir
totalFilescount = diSource.GetFiles("*", SearchOption.AllDirectories).Length;
In CopyAll method, for each file copied you increment currentFileindex, then you can calculate progress using the following
double progressVal = (double)(currentFileindex * 100 )/ totalFilescount;
Modified CopyAll method to report progress
public void CopyAll(DirectoryInfo source, DirectoryInfo target)
{
DirectoryInfo di = Directory.CreateDirectory(target.FullName);
// Copy each file into the new directory.
foreach (FileInfo fi in source.GetFiles())
{
Console.WriteLine(#"Copying {0}\{1}", target.FullName, fi.Name);
fi.CopyTo(Path.Combine(target.FullName, fi.Name), true);
currentFileindex += 1;
double progressVal = (double)(currentFileindex * 100 )/ totalFilescount;
if (progressVal <= 100)
{
progressBar1.Value =Convert.ToInt32(Math.Floor(progressVal));
}
}
// Copy each subdirectory using recursion.
foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
{
DirectoryInfo nextTargetSubDir =
target.CreateSubdirectory(diSourceSubDir.Name);
CopyAll(diSourceSubDir, nextTargetSubDir);
}
}
In one of my plugins in my eclipse RCP I'm storing some property files.
Package: com.demo.my.package contains two files:
File1.properties
File2.properties
How can I copy, during runtime, the content of the com.demo.my.package to an arbitary place on the filesystem?
Note: I don't know how many files are in the com.demo.my.package.
I just gave two filenames as an example.
So far I have been trying to get an URL instance of the package folder using
URL url = Platform.getBundle(pluginId).getEntry(path);
where pluginID is the ID of the plugin of the package and path is the following String:
com/demo/my/package
This call results in a null value so I'm stuck here.
It may be an easy task if I just deploy the plugin in a flat structure but I would prefere to not do that.
Any ideas how to copy the content of a package to another place in the filesystem during runtime?
Solution:
Create a new scource folder called texts in the root of the plugin which contains the files to copy.
Gain URL of the source folder via my custom function:
public String getLocalFilePath(String pluginId, String path) {
URL url = Platform.getBundle(pluginId).getEntry(path);
try {
String filepath = FileLocator.toFileURL(url).toString();
filepath = filepath.replace("/", "\\");
filepath = filepath.substring(6, filepath.length());
return filepath;
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return null;
}
Copy files
File sourceDirectory = new File(source);
File[] listOfFiles = sourceDirectory.listFiles();
for (int i = 0; i < listOfFiles.length; i++) {
String sourcePath = source + "\\" + listOfFiles[i].getName();
String destPath = targetDirectoryString + "\\" + listOfFiles[i].getName();
try {
copyFileUsingChannel(new File(sourcePath), new File(destPath));
} catch (IOException e) {
e.printStackTrace();
}
}
private static void copyFileUsingChannel(File source, File dest) throws IOException {
FileChannel sourceChannel = null;
FileChannel destChannel = null;
try {
sourceChannel = new FileInputStream(source).getChannel();
destChannel = new FileOutputStream(dest).getChannel();
destChannel.transferFrom(sourceChannel, 0, sourceChannel.size());
} finally {
sourceChannel.close();
destChannel.close();
}
}
I have a WebBrowser control on a form, but for the most part it remains hidden from the user. It is there to handle a series of login and other tasks. I have to use this control because there is a ton of Javascript that handles the login. (i.e., I can't just switch to a WebClient object.)
After hopping around a bit, we end up wanting to download a PDF file. But instead of downloading, the file is displayed within the webBrowser control, which the user can not see.
How can I download the PDF instead of having it load in the browser control?
Add a SaveFileDialog control to your form, then add the following code on your WebBrowser's Navigating event:
private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
if (e.Url.Segments[e.Url.Segments.Length - 1].EndsWith(".pdf"))
{
e.Cancel = true;
string filepath = null;
saveFileDialog1.FileName = e.Url.Segments[e.Url.Segments.Length - 1];
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
filepath = saveFileDialog1.FileName;
WebClient client = new WebClient();
client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);
client.DownloadFileAsync(e.Url, filepath);
}
}
}
//Callback function
void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
MessageBox.Show("File downloaded");
}
Source: http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/d338a2c8-96df-4cb0-b8be-c5fbdd7c9202
The solution I ended up using:
I did everything else as-needed to get the URL where it needed to go. Knowing that all of the login information, required settings, viewstates, etc. were stored in the cookies, I was finally able to grab the file using a hybrid of the web control to navigate then the WebClient object to actually snag the file bytes.
public byte[] GetPDF(string keyValue)
{
DoLogin();
// Ask the source to generate the PDF. The PDF doesn't
// exist on the server until you have visited this page
// at least ONCE. The PDF exists for five minutes after
// the visit, so you have to snag it pretty quick.
LoadUrl(string.Format(
"https://www.theMagicSource.com/getimage.do?&key={0}&imageoutputformat=PDF",
keyValue));
// Now that we're logged in (not shown here), and
// (hopefully) at the right location, snag the cookies.
// We can use them to download the PDF directly.
string cookies = GetCookies();
byte[] fileBytes = null;
try
{
// We are fully logged in, and by now, the PDF should
// be generated. GO GET IT!
WebClient wc = new WebClient();
wc.Headers.Add("Cookie: " + cookies);
string tmpFile = Path.GetTempFileName();
wc.DownloadFile(string.Format(
"https://www.theMagicSource.com/document?id={0}_final.PDF",
keyValue), tmpFile);
fileBytes = File.ReadAllBytes(tmpFile);
File.Delete(tmpFile);
}
catch (Exception ex)
{
// If we can't get the PDF here, then just ignore the error and return null.
throw new WebScrapePDFException(
"Could not find the specified file.", ex);
}
return fileBytes;
}
private void LoadUrl(string url)
{
InternalBrowser.Navigate(url);
// Let the browser control do what it needs to do to start
// processing the page.
Thread.Sleep(100);
// If EITHER we can't continue OR
// the web browser has not been idle for 10 consecutive seconds yet,
// then wait some more.
// ...
// ... Some stuff here to make sure the page is fully loaded and ready.
// ... Removed to reduce complexity, but you get the idea.
// ...
}
private string GetCookies()
{
if (InternalBrowser.InvokeRequired)
{
return (string)InternalBrowser.Invoke(new Func<string>(() => GetCookies()));
}
else
{
return InternalBrowser.Document.Cookie;
}
}
bool documentCompleted = false;
string getInnerText(string url)
{
documentCompleted = false;
web.Navigate(url);
while (!documentCompleted)
Application.DoEvents();
return web.Document.Body.InnerText;
}
private void web_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
documentCompleted = true;
}