Commit all files in a folder to SVN repository using sharpsvn - winforms

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.

Related

How can I write Parallel and Interlock using function for WPF

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?

Application Level Unhandled Exceptions Not Creating New File

I have a bit of code in my App.cs file to catch unhandled exceptions in the event of a crash. The problems is when I step through the code, it reads as if the file exists, but no new file is actually created. I am probably overlooking something simple but I could use a second (or more ;)) set of eyes on it. Here is my method.
private void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
var contents =
string.Format(
"HResult: {1}{0}" + "HelpLink: {2}{0}" + "Message: {3}{0}" + "Source: {4}{0}"
+ "StackTrace: {5}{0}" + "{0}",
Environment.NewLine,
e.Exception.HResult,
e.Exception.HelpLink,
e.Exception.Message,
e.Exception.Source,
e.Exception.StackTrace);
if (!File.Exists("/Source/CrashLogs/Exceptions.txt"))
{
File.WriteAllText("/Source/CrashLogs/Exceptions.txt", contents);
}
else
{
File.AppendAllText("/Source/CrashLogs/Exceptions.txt", contents);
}
e.Handled = true;
}
I'll next expand this out so that the file name contains the current date, but I would just like to get it running first.
Thanks!!
is folder /Source/CrashLogs/ already exists?
handle also AppDomain.CurrentDomain.UnhandledException
more info here

File Watcher in VSPackage

I'm new in developing in Visual Studio so I hope my problem is not too stupid :-)
I try to establish a FileWatcher in my VSPackage to detect any changes performed in the current solution.
For this I found the class FileWatcher, which - in my opinion - detect any file changes.
But anything in my code must be wrong as no event is fired.
Maybe you can help me?
The solution I want to watch is in D:\\Test but entering the specific path doesn't help
class Watcher
{
FileSystemWatcher watcher = new FileSystemWatcher();
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
public void StartWatching()
{
Debug.WriteLine("in watcher method");
watcher.Path = #"D:\\";
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName;
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.Created += new FileSystemEventHandler(OnChanged);
watcher.Deleted += new FileSystemEventHandler(OnChanged);
watcher.Renamed += new RenamedEventHandler(OnRenamed);
watcher.EnableRaisingEvents = true;
}
private static void OnChanged(object source, FileSystemEventArgs e)
{
Debug.WriteLine("Changes in folder:" + e.FullPath);
}
private static void OnRenamed(object source, RenamedEventArgs e)
{
Debug.WriteLine("File: {0} renamed to {1}", e.OldFullPath, e.FullPath);
}
private static void OnDeleted(object source, RenamedEventArgs e)
{
Debug.WriteLine("deleted");
}
private static void OnCreated(object source, RenamedEventArgs e)
{
Debug.WriteLine("created");
}
}
In the initialize method of my VSPackage i call:
("in watcher method" is displayed in output so this part should work)
watch = new Watcher();
watch.StartWatching();
Tank you in advance!
EDIT
I found out, that the path at the test environment changes.
But now I have the problem, that I'm not able to get the name of the changed file, as the change event only gives me .opensdf or .sdf
Can you help me?
Instead of watching the file changes yourself you can use the built in events provided by Visual Studio: SolutionEvents and ProjectItemEvents. This Stack Overflow post explains how to use them in a vs package.

Eclispe RCP: Copying files from package folder of a plugin into an arbitary place of the filesystem

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();
}
}

Download a file through the WebBrowser control

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;
}

Resources