FileExplorer using treeview - treeview

I have ImageList and TreeView. I created a folder which contains three folders and each folder contains three text files. When I start debugging it, it just has the root folder and unable to expand it to see the other folders and files. Any ideas on how to fix it.
using System.IO;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
DirectoryInfo directoryinfo = new DirectoryInfo(#"C:\Users\zaid\Documents\Visual Studio 2010\Projects\file_explorer");
private void Form1_Load(object sender, EventArgs e)
{
if (Directory.Exists("file_explorer"))
{
try
{
DirectoryInfo[] directories = directoryinfo.GetDirectories();
if (directories.Length > 0)
{
foreach (DirectoryInfo directory in directories)
{
TreeNode node = treeView1.Nodes[0].Nodes.Add(directory.Name);
node.ImageIndex = 1;
foreach (FileInfo file in directory.GetFiles())
{
if (file.Exists)
{
TreeNode nodes = treeView1.Nodes[0].Nodes[node.Index].Nodes.Add(file.Name);
nodes.ImageIndex = 2;
}
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
}

Related

Bukkit - How to reload custom yaml-configuration

Okay, so I'm trying to make a toggleable feature, whether they have it enabled/disabled is stored in the 'data.yml'. The issue I have with this is that the file does get updated (Asin, it does change from true to false and vice versa) but it doesn't actually apply the changes in-game.
Method for reloading the file:
public static void reloadConfig(File file, FileConfiguration conf) {
try {
conf.save(file);
} catch (Exception e) {
}
conf = YamlConfiguration.loadConfiguration(file);
}
Toggle command:
if (args[0].equalsIgnoreCase("toggle")) {
File file = new File("plugins/StatTrack", "data.yml");
if (file.exists()) {
FileConfiguration conf = YamlConfiguration.loadConfiguration(file);
if (conf.getBoolean("Users." + player.getName() + ".OreTracker") == true) {
conf.set("Users." + player.getName() + ".OreTracker", false);
try {
Main.reloadConfig(file, conf);
Main.message(player, "&cDisabled&f the Ore&8-&fTracker");
return true;
} catch (Exception e) {
Main.message(player, "&cSome fatal error occored");
return true;
}
} else if (conf.getBoolean("Users." + player.getName() + ".OreTracker") == false) {
conf.set("Users." + player.getName() + ".OreTracker", true);
try {
Main.reloadConfig(file, conf);
Main.message(player, "&aEnabled&f the Ore&8-&fTracker");
return true;
} catch (Exception e) {
Main.message(player, "&cSome fatal error occored");
return true;
}
}
}
}
If you need any more code or have any questions I'll happily supply the code/answer.
Thanks in advance.
The problem is that the plugin is saving the config in the reload method. I also wouldn't recommend using a static method in this case unless the class of the method is a Singleton.
So let's create a new class being a Singleton. The Singleton pattern describes a class which has only one instance accessible through static methods.
public class PluginConfig {
private static PluginConfig instance; // Static (global) reference to the instance
File confFile;
YamlConfiguration conf;
public PluginConfig(File confFile) {
this.confFile = confFile;
loadConfig();
}
public static YamlConfiguration getConfig() {
return instance.conf;
}
public static void loadConfig() {
instance.conf = YamlConfiguration.loadConfiguration(confFile);
}
// Extra method for another implementation, if potentially needed in the future
public static void reloadConfig() {
loadConfig();
}
}
Using that class you can access the config from everywhere with PluginConfig.getConfig()

I can't make a basic project run with javaexe

I created a basic program in order to see how to make javaexe run;
The program has to be a windows service and should display a icon on the taskbar, when one click in the menu item, a messageBox is displayed.
But it does not run, especially the icon is not displayed.
Do you know why?
here are my files:
the service:
package service;
/**
* Created by User on 26/10/2014.
*/
public class Exemple_ServiceManagement {
static Corps app=null;
public static boolean serviceInit (){
app=new Corps();
return true;
}
public static void serviceFinish (){
app.setEnd(true);
}
public static String[] serviceGetInfo (){
return new String[]{"mon exemple","test","pas auto",
"1"};
}
}
the core part:
public class Corps extends Thread{
public boolean isEnd() {
return isEnd;
}
public void setEnd(boolean isEnd) {
this.isEnd = isEnd;
}
public boolean isEnd=false;
/*******************************************/
public Corps()
{
start();
}
/*******************************************/
public void run(){
while (!isEnd){
try {
Thread.sleep(250);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
and the taskbar part:
public class Exemple_TaskbarManagement {
public static void taskInit() {
}
public static String[][] taskGetMenu(boolean isRightClick, int menuID) {
return new String[][]
{
{"1", "test" , "", ""},
};
}
public static void taskDoAction (boolean isRightClick, int menuID){
if (!isRightClick && menuID==1){
javax.swing.JOptionPane.showMessageDialog(null,"Menu cliqué");
}
}
public static String[] taskGetInfo (){
return new String[]
{
"JavaExe : mon exemple",
"",
"",
"",
""
};
}
}
finally, the output directory contains:
- the file "Exemple.exe" (the javaexe exe)
- the file "Exemple.jar", generated from the above sources, without manifest
- the file "Exemple.properties", here is it:
JREversion =
PersonalClasspath =
MainArgs =
MainClass =
PersonalOptions =
RunAsService =
RunType =1
ClassDirectory =
ResourceDirectory =
URL_InstallJRE =
Display_BoxInstall =
PathBrowser =
PathJRE =
my question is very simple : do you know what's going wrong?
thanks
olivier
ps : I had a look at the example files but I want to make a basic project, not take an other from somebody else, in order to understand what I do.

Weird and Annoying IsolatedStorageError

Update
I added the piece of code/function that normally throws the error.
can someone please help.
Before I begin, I have spent over 20+ hours researching this issue.
I am developing a windows phone app and I keep getting a lot of IsolatedStorage Exceptions, especially "Operation not supported on IsolatedStorageFileStream".
The Scenario
I have an object that has a ProfilePictureUrl as a property, every time I create an instance of the object I download the profile image from the web, then I will store that image to the Isolated storage.
Sample Code
foreach(String url in urls)
{
var profile = new MyClass()
{
ProfilePictureURL = url
};
profile.DownloadProfilePictureToLocalStorage(() =>
{
completed(profile);
}, (ex) => { incomplete(ex); });
}
The Code that throws the Exception
if (isoFile.FileExists(saveAs))
isoFile.DeleteFile(saveAs);
using (var isoFileStream = isoFile.CreateFile(saveAs))
{
var width = wb.PixelWidth;
var height = wb.PixelHeight;
Extensions.SaveJpeg(wb, isoFileStream, width, height, 0, 100);
}
This is inside the DownloadImageToIsolatedStorage function in the LocalStorageManager class.
Here is the class that manages my IsolatedStorage storing
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows.Media.Imaging;
namespace Classes.Managers
{
public delegate void GetImageCompletedDelegate(BitmapImage bmp);
public delegate void GetImageNotCompletedDelegate(Exception ex);
public delegate void SaveImageCompletedDelegate();
public delegate void SaveImageNotCompletedDelegate(Exception ex);
public delegate void DeleteImageCompletedDelegate();
public delegate void DeleteImageNotCompletedDelegate(Exception ex);
class LocalStorageManager
{
private static readonly LocalStorageManager _instance = new LocalStorageManager();
public static LocalStorageManager Instance
{
get
{
return _instance;
}
}
private bool m_IsBusy;
public bool IsBusy
{
get { return m_IsBusy; }
private set { m_IsBusy = value; }
}
private void GetImageFromIsolatedStorage(String name, GetImageCompletedDelegate completed, GetImageNotCompletedDelegate notCompleted)
{
try
{
using (IsolatedStorageFile isoFile = IsolatedStorageFile.GetUserStoreForApplication())
{
BitmapImage img = new BitmapImage();
if (isoFile.FileExists(name))
{
using (IsolatedStorageFileStream fileStream = isoFile.OpenFile(name, FileMode.Open, FileAccess.Read))
{
img.SetSource(fileStream);
}
}
completed(img);
}
}
catch (Exception ex) { notCompleted(ex); }
}
public void DownloadImageToIsolatedStorage(String url, string saveAs, SaveImageCompletedDelegate completed, SaveImageNotCompletedDelegate notCompleted)
{
try
{
this.IsBusy = true;
HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(url);
httpRequest.BeginGetResponse((callback) =>
{
System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =>
{
using (IsolatedStorageFile isoFile = IsolatedStorageFile.GetUserStoreForApplication())
{
HttpWebResponse response = (HttpWebResponse)httpRequest.EndGetResponse(callback);
var bi = new BitmapImage();
bi.SetSource(response.GetResponseStream());
var wb = new WriteableBitmap(bi);
if (isoFile.FileExists(saveAs))
isoFile.DeleteFile(saveAs);
using (var isoFileStream = isoFile.CreateFile(saveAs))
{
var width = wb.PixelWidth;
var height = wb.PixelHeight;
Extensions.SaveJpeg(wb, isoFileStream, width, height, 0, 100);
}
this.IsBusy = false;
completed();
}
});
}, null);
}
catch (Exception e) { this.IsBusy = false; notCompleted(e); }
}
public void MoveFile(String source, String destination)
{
using (IsolatedStorageFile isoFile = IsolatedStorageFile.GetUserStoreForApplication())
{
if (isoFile.FileExists(source))
{
isoFile.MoveFile(source, destination);
}
}
}
private void WriteImageStreamToIsolatedStorage(Stream imageStream, string saveAs, SaveImageCompletedDelegate completed, SaveImageNotCompletedDelegate notCompleted)
{
try
{
using (var isolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
if (isolatedStorage.FileExists(saveAs))
isolatedStorage.DeleteFile(saveAs);
var fileStream = isolatedStorage.CreateFile(saveAs);
imageStream.CopyTo(fileStream);
fileStream.Close();
/*
BitmapImage bmp = null;
bmp.SetSource(imageStream);
var writeableBMP = new WriteableBitmap(bmp);
writeableBMP.SaveJpeg(fileStream, writeableBMP.PixelWidth, writeableBMP.PixelHeight, 0, 100);
fileStream.Close();*/
}
completed();
}
catch (Exception ex) { notCompleted(ex); }
}
private void DeleteImageFromIsolatedStorage(string imageName, DeleteImageCompletedDelegate completed, DeleteImageNotCompletedDelegate notCompleted)
{
try
{
using (var isolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
if (isolatedStorage.FileExists(imageName))
isolatedStorage.DeleteFile(imageName);
completed();
}
}
catch (Exception e) { notCompleted(e); }
}
}
}
Here is the class for my objects
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Media.Imaging;
using System.Runtime.Serialization;
using System.IO.IsolatedStorage;
using System.Net;
namespace Classes.Model
{
public class MyClass : INotifyPropertyChanged
{
private string m_ProfilePictureURL;
public string ProfilePictureURL
{
get { return m_ProfilePictureURL; }
set
{
if (m_ProfilePictureURL == value)
return;
m_ProfilePictureURL = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("ProfilePictureURL"));
}
}
[IgnoreDataMember]
public BitmapImage LocalProfilePicture
{
get
{
using (IsolatedStorageFile isoFile = IsolatedStorageFile.GetUserStoreForApplication())
{
try
{
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.SetSource(isoFile.OpenFile("ProfilePic_" + this.UUID, System.IO.FileMode.Open, System.IO.FileAccess.ReadWrite, System.IO.FileShare.ReadWrite));
return bitmapImage;
}
catch (Exception ex)
{
isoFile.Dispose();
return null;
}
}
}
}
public void DownloadProfilePictureToLocalStorage(SaveImageCompletedDelegate completed, SaveImageNotCompletedDelegate notCompleted)
{
// Wait till its no longer busy
while (LocalStorageManager.Instance.IsBusy) ;
if (!String.IsNullOrWhiteSpace(this.ProfilePictureURL) && !String.IsNullOrWhiteSpace(this.UUID))
{
LocalStorageManager.Instance.DownloadImageToIsolatedStorage(this.ProfilePictureURL, "ProfilePic_" + this.UUID, () =>
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("LocalProfilePicture"));
completed();
}, (ex) => { notCompleted(ex); });
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
}

how do i import my jar file in drjava?

i have already added the file into extra class path but when i try to call the method "boolean ImageLibary.loadImage(File f)" i get error cannot find symbol..
public class ImageEditing
{
public static void main(String[]args) throws Exception
{
File image = getfile();
boolean i= ImageLibary.loadImage(image); // error occurs here
}
public static File getfile()
{
JFileChooser chooser = new JFileChooser();
try
{
String currentDir = System.getProperty("user.dir");
chooser.setCurrentDirectory(new File(currentDir));
}
catch (Exception e)
{
System.out.println("Warning: Error setting current folder: "+e.getMessage());
}
File file = null;
int returnValue;
returnValue = chooser.showOpenDialog(null);
if(returnValue==JFileChooser.APPROVE_OPTION)
{
file = chooser.getSelectedFile();
}
return file;
}
}

Toolbox items grayed out in VS 2010

I have tried numerous attempts to fix this problem or bug, firstly by deleting the .tbd files from C:\Users\\AppData\Local\Microsoft\VisualStudio\x.0
I have also tried this:
Visual Studio "Tools" menu
"Options" submenu
"Windows Form Designer" tab
"General" tab
Set "AutoToolboxPopulate" to "True"
The ToolBox list is still not populating correctly and the "BackgroundWorker" component I need is grayed out. Any ideas?
At least a workaround: declare the BackgroundWorker in code, but don't forget to dispose it properly:
public class MyForm : Form
{
private BackgroundWorker bgWorker = null;
public MyForm()
{
InitializeComponent();
this.bgWorker = new BackgroundWorker; //TODO: set properties and event handlers
}
public override void Dispose(bool disposing)
{
//TODO: copy from MyForm.Designer.cs and add:
Backgroundworker bgw = this.bgWorker;
this.bgWorker = null;
if (disposing && bgw != null)
{
try
{
//TODO: release event handlers
bgw.Dispose();
}
catch(Exception)
{
/* consumed disposal error */
}
}
}
}
I have found a solution to my problem, using the BackgroundWorker class in C# without using the component from the toolbox. In this case, I needed two seperate backgroundWorkers:
using System.Threading;
public partial class MainWindow : Window
{
private BackgroundWorker bw1 = new BackgroundWorker();
private BackgroundWorker bw2 = new BackgroundWorker();
public MainWindow()
{
InitializeComponent();
bw1.WorkerReportsProgress = true;
bw1.DoWork += new DoWorkEventHandler(bw1_DoWork);
bw1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw1_RunWorkerCompleted);
bw1.ProgressChanged += new ProgressChangedEventHandler(bw1_ProgressChanged);
bw2.WorkerReportsProgress = true;
bw2.DoWork += new DoWorkEventHandler(bw2_DoWork2);
bw2.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw2_RunWorkerCompleted);
bw2.ProgressChanged += new ProgressChangedEventHandler(bw1_ProgressChanged);
}
private void bw1_DoWork(object sender, DoWorkEventArgs e)
{
StatsProcessor proc = new StatsProcessor();
proc.CompareStats(listText1, listText2);
}
private void bw2_DoWork2(object sender, DoWorkEventArgs e)
{
StatsParser parser = new StatsParser();
}
private void bw1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
progressBar2.IsIndeterminate = false;
progressBar2.Value = 100;
btnCompareStats.IsEnabled = true;
}
private void bw2_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
progressBar1.IsIndeterminate = false;
progressBar1.Value = 100;
btnFetchStats.IsEnabled = true;
}
private void bw1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
this.progressBar2.Value = e.ProgressPercentage;
}
private void bw2_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
this.progressBar1.Value = e.ProgressPercentage;
}
private void btnCompare_Click(object sender, EventArgs e)
{
btnCompareStats.IsEnabled = false;
StatsProcessor proc = new StatsProcessor();
if (bw1.IsBusy != true)
{
progressBar2.IsIndeterminate = true;
// Start the asynchronous operation.
bw1.RunWorkerAsync();
}
}
private void btnFetchStats_Click(object sender, RoutedEventArgs e)
{
btnFetchStats.IsEnabled = false;
if (bw2.IsBusy != true)
{
progressBar1.IsIndeterminate = true;
// Start the asynchronous operation.
bw2.RunWorkerAsync();
}
}
}
I would try resetting the toolbox items. Then use the Add Item dialog to put back something you need.

Resources