How to detect new or modified files - winapi

I found there are two ways to approach it, ReadDirectoryChangesW and FindFirstChangeNotification.
I want to know what’s the difference between them, performance or otherwise?

This is tagged for Winapi, but I give it a shot with .net anyway.
You can use the FileSystemWatcher class from System.IO for this.
static void Main(string[] args)
{
FileSystemWatcher fsw = new FileSystemWatcher("C:\\");
fsw.Changed += new FileSystemEventHandler(fsw_Changed);
fsw.Created += new FileSystemEventHandler(fsw_Created);
fsw.Deleted += new FileSystemEventHandler(fsw_Deleted);
fsw.EnableRaisingEvents = true;
fsw.IncludeSubdirectories = true;
fsw.Renamed += new RenamedEventHandler(fsw_Renamed);
Console.Read();
}
static void fsw_Renamed(object sender, RenamedEventArgs e)
{
Console.WriteLine("{0} was changed to {1}", e.OldName, e.Name);
}
static void fsw_Deleted(object sender, FileSystemEventArgs e)
{
Console.WriteLine("{0} was deleted", e.Name);
}
static void fsw_Created(object sender, FileSystemEventArgs e)
{
Console.WriteLine("{0} was created", e.Name);
}
static void fsw_Changed(object sender, FileSystemEventArgs e)
{
Console.WriteLine("{0} was changed", e.Name);
}

Related

Fault Exception windows phone connecting to WCF WP8

I have created a WCF which I then created a WP8 application and added the service reference. The error is "An exception of type 'System.ServiceModel.FaultException' occurred in System.ServiceModel.ni.dll but was not handled in user code"
If anyone could possible give me some indication as I have googled this and found nothing the same.
The error comes up here
public void EndAddUser(System.IAsyncResult result)
{
object[] _args = new object[0];
base.EndInvoke("AddUser", _args, result);
}
My main classes:
public partial class MainPage : PhoneApplicationPage
{
private Service1Client _serviceClient;
// Constructor
public MainPage()
{
InitializeComponent();
_serviceClient = new Service1Client();
_serviceClient.LoginUserCompleted += new
}
private void loginBtn_Click(object sender, RoutedEventArgs e)
{
_serviceClient.LoginUserAsync(userNameTxtBox.Text, passwordTxtBox.Text);
}
private void newAccountBtn_Click(object sender, RoutedEventArgs e)
{
this.NavigationService.Navigate(new Uri("/AddAccount.xaml", UriKind.Relative));
}
private void _serviceClient_LoginUserCompleted(object senter,
LoginUserCompletedEventArgs e)
{
if (e.Error == null && e.Result != null)
{
MessageBox.Show("Welcome " + e.Result + "!");
}
else
{
MessageBox.Show("Could not log in. Please check user name/password and try
again.");
}
}
}
public partial class AddAccount : PhoneApplicationPage
{
private Service1Client _serviceClient;
public AddAccount()
{
InitializeComponent();
_serviceClient = new Service1Client();
_serviceClient.AddUserCompleted += new EventHandler<AsyncCompletedEventArgs>
(_serviceClient_AddUserCompleted);
}
private void addAccountBtn_Click(object sender, RoutedEventArgs e)
{
_serviceClient.AddUserAsync(fullnameTxtBox.Text, userNameTxtBox.Text,
passwordTxtBox.Text);
}
void _serviceClient_AddUserCompleted(object sender, AsyncCompletedEventArgs e)
{
if (e.Error == null)
{
MessageBox.Show("User account created. Please login");
this.NavigationService.GoBack();
}
else
{
MessageBox.Show("User account could not be added, Please try again");
}
}
}

photo chooser task windows phone 7.1

i need to take a picture from the directory of Windows phone in my application so i add a child Windows when i click on the text in this child Windows i add a button and i called
public partial class AnnotationControl : ChildWindow
{
public ObservableCollection<string> cercle { get; set; }
public AnnotationControl()
{
InitializeComponent();
}
private void ChildWindow_Closing_1(object sender, System.ComponentModel.CancelEventArgs e)
{
}
private void btnsave_Click_1(object sender, RoutedEventArgs e)
{
this.DialogResult = true;
}
private void btnCancel_Click_1(object sender, RoutedEventArgs e)
{
this.DialogResult = true;
}
private void browse_Click(object sender, RoutedEventArgs e)
{
PhotoChooserTask objPhotoChooser = new PhotoChooserTask();
objPhotoChooser.Completed += new EventHandler<PhotoResult>(PhotoChooseCall);
objPhotoChooser.Show();
}
but when i clicked on the button to choose a picture the application crashed
"Application_UnhandledException"
some one have any idea please
Use this may be helpful
private void changeImage(object sender, RoutedEventArgs e)
{
PhotoChooserTask photoChooserTask = new PhotoChooserTask();
photoChooserTask.Completed += new EventHandler<PhotoResult>(photoChooserTask_Completed);
photoChooserTask.Show();
}
void photoChooserTask_Completed(object sender, PhotoResult e)
{
if (e.TaskResult == TaskResult.OK)
{
System.Windows.Media.Imaging.BitmapImage bmp = new System.Windows.Media.Imaging.BitmapImage();
bmp.SetSource(e.ChosenPhoto);
imageTapped.Source = bmp;
}
}

Need slideshow of images when my application is launched in windows phone 7

How to get the slideshow of images with time interval of 2 seconds. I had referred the below code from stackoverflow but while running iam nt getting any images displayed.. Please tel where iam went wrong...
Xaml:
<image Name=myImg Source={Binding bi}/>
Code:
private DispatcherTimer tmr = new DispatcherTimer();
private List<string> images = new List<string>();
private int imageIndex = 0;
public MainPage()
{
InitializeComponent();
Loaded += new RoutedEventHandler(MainPage_Loaded);
}
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
tmr.Interval = TimeSpan.FromSeconds(5);
tmr.Tick += new EventHandler(tmr_Tick);
LoadImages();
ShowNextImage();
}
private void LoadImages()
{
// list the files (includede in the XAP file) here
images.Add("/images/filename1.jpg");
images.Add("/images/filename2.jpg");
images.Add("/images/filename3.jpg");
images.Add("/images/filename4.jpg");
}
private void ShowNextImage()
{
Imagesource bi = new BitmapImage(new Uri(images[imageIndex], UriKind.Relative));
myImg.Source = bi;
imageIndex = (imageIndex + 1) % images.Count;
}
void tmr_Tick(object sender, EventArgs e)
{
ShowNextImage();
}
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
if (!tmr.IsEnabled)
{
tmr.Start();
}
base.OnNavigatedTo(e);
}
protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
{
tmr.Stop();
base.OnNavigatedFrom(e);
}
Thanks in Advance
Change Name=myImg to x:Name="myImg" and remove the Source attribute entirely. Otherwise it looks like it should work.

MediaElement repeatedly play Exception

I'm new in WP7, and I am trying to play sounds with a MediaElement when I press a button.
Its works, but unfortunately I get "Operation not permitted on IsolatedStorageFileStream" Exception when I press the button repeatedly, before the sound begins. How can I avoid that?
The Play method:
public void Play(string filename)
{
try
{
mediaElement.Stop();
mediaElement.ClearValue(MediaElement.SourceProperty);
using (var isf = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream fileStream = isf.OpenFile(#"shared\transfers\" + filename, FileMode.Open))
{
mediaElement.SetSource(fileStream);
mediaElement.IsMuted = false;
mediaElement.Volume = 1.0;
}
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
Event handlers:
void me_MediaOpened(object sender, RoutedEventArgs e)
{
mediaElement.Play();
}
void me_MediaEnded(object sender, RoutedEventArgs e)
{
mediaElement.ClearValue(MediaElement.SourceProperty);
}
If you'd like for the user to be allowed to click the play button only once. One way would be.
public bool IsPlaying = false;
void me_MediaOpened(object sender, RoutedEventArgs e)
{
if(!IsPlaying){
mediaElement.Play();
IsPlaying = true;
}
}
void me_MediaEnded(object sender, RoutedEventArgs e)
{
if(IsPlaying){
mediaElement.ClearValue(MediaElement.SourceProperty);
IsPlaying = false;
}
}
Loading the filestream to a memorystream, before playing should make it more flexible, if you're dealing with low size streams.

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