2013-06-10 4 views
3

이 응용 프로그램을 실행하려고하면 "경로가 올바른 형식이 아닙니다."라는 메시지가 표시됩니다. 경고이며 "FileSystemWatcher1.IncludeSubdirectories = true;"와 함께 잘못된 것이 있다고합니다. 찾아보기를 클릭하면 두 번째 파일 탐색기에서 찾아보기를 클릭하면 정확히 동일합니다. (나는 2 개의 디렉토리를보기 위해 2 개의 브라우즈 버튼을 가지고있다.) Filewatchers에게 시작 경로가 없었지만, 시작 경로를 주면 작동한다. 나는 그것을 원하지 않는다. 도와주세요.경로가 합법적 인 형식이 아닙니다.

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.IO; 

namespace WindowsFormsApplication1 
{ 
public partial class Form1 : Form 
{ 
    private bool pause = false; 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 

    } 

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
    } 


    // The lines with performed actions of a file 
    private void fileSystemWatcher1_Created(object sender, System.IO.FileSystemEventArgs e) 
    { 
     if (!pause) 
     { 
      listBox1.Items.Add("File Created> " + e.FullPath + " -Date:" + DateTime.Now); 
     } 
    } 

    private void fileSystemWatcher1_Changed(object sender, System.IO.FileSystemEventArgs e) 
    { 
     if (!pause) 
     { 
      listBox1.Items.Add("File Changed> " + e.FullPath + " -Date:" + DateTime.Now); 
     } 
    } 
    private void fileSystemWatcher1_Deleted(object sender, System.IO.FileSystemEventArgs e) 
    { 
     if (!pause) 
     { 
      listBox1.Items.Add("File Deleted> " + e.FullPath + " -Date:" + DateTime.Now); 
     } 
    } 

    private void fileSystemWatcher1_Renamed(object sender, System.IO.RenamedEventArgs e) 
    { 
     if (!pause) 
     { 
      listBox1.Items.Add("File Renamed> " + e.FullPath + " -Date:" + DateTime.Now); 
     } 
    } 

    private void fileSystemWatcher2_Changed(object sender, System.IO.FileSystemEventArgs e) 
    { 
     if (!pause) 
     { 
      listBox1.Items.Add("File Changed> " + e.FullPath + " -Date:" + DateTime.Now); 
     } 
    } 

    private void fileSystemWatcher2_Created(object sender, System.IO.FileSystemEventArgs e) 
    { 
     if (!pause) 
     { 
      listBox1.Items.Add("File Created> " + e.FullPath + " -Date:" + DateTime.Now); 
     } 
    } 

    private void fileSystemWatcher2_Deleted(object sender, System.IO.FileSystemEventArgs e) 
    { 
     if (!pause) 
     { 
      listBox1.Items.Add("File Deleted> " + e.FullPath + " -Date:" + DateTime.Now); 
     } 
    } 

    private void fileSystemWatcher2_Renamed(object sender, System.IO.RenamedEventArgs e) 
    { 
     if (!pause) 
     { 
      listBox1.Items.Add("File Renamed> " + e.FullPath + " -Date:" + DateTime.Now); 
     } 
    } 

    //1st directory 
    private void button2_Click(object sender, EventArgs e) 
    { 
     fileSystemWatcher1.IncludeSubdirectories = true; 
     DialogResult resDialog = dlgOpenDir.ShowDialog(); 
     if (resDialog.ToString() == "OK") 
     { 
      fileSystemWatcher1.Path = dlgOpenDir.SelectedPath; 
      textBox1.Text = dlgOpenDir.SelectedPath; 
     } 
    } 
    //2nd directory 
    private void button3_Click(object sender, EventArgs e) 
    { 
     fileSystemWatcher2.IncludeSubdirectories = true; 
     DialogResult resDialog = dlgOpenDir.ShowDialog(); 
     if (resDialog.ToString() == "OK") 
     { 
      fileSystemWatcher2.Path = dlgOpenDir.SelectedPath; 
      textBox2.Text = dlgOpenDir.SelectedPath; 
     } 
    } 
    //log 
    private void button1_Click(object sender, EventArgs e) 
    { 

     DialogResult resDialog = dlgSaveFile.ShowDialog(); 
     if (resDialog.ToString() == "OK") 
     { 
      FileInfo fi = new FileInfo(dlgSaveFile.FileName); 
      StreamWriter sw = fi.CreateText(); 
      foreach (string sItem in listBox1.Items) 
      { 
       sw.WriteLine(sItem); 
      } 
      sw.Close(); 
     } 
    } 
    //pause watching 
    private void pause_button_Click(object sender, EventArgs e) 
    { 
     if (!pause) 
     { 
      pause = true; 
      pause_button.Text = "Unpause"; 
     } 
     else 
     { 
      pause = false; 
      pause_button.Text = "Pause Watching"; 
     } 
    } 
    //clear listbox 
    private void clear_button_Click(object sender, EventArgs e) 
    { 
     listBox1.Items.Clear(); 
    } 
} 

은}

+1

은 충돌 하는가? – rene

답변

6

그냥 추측을 읽기 위해이

try 
{ 
    // Your code goes here 
} 
catch(Exception ex) 
{ 
    // If exception raise compiler comes here.. 
} 

같은 catch 블록에 예외를 catch, 하지만 및 Path을 변경하기 전에 EnableRaisingEvents을 false로 설정해야할까요? 이처럼 : 어떤 줄에

private void button2_Click(object sender, EventArgs e) 
{ 
    if (dlgOpenDir.ShowDialog() == DialogResult.OK) 
    { 
     fileSystemWatcher1.EnableRaisingEvents = false; // Stop watching 
     fileSystemWatcher1.IncludeSubdirectories = true; 
     fileSystemWatcher1.Path = dlgOpenDir.SelectedPath; 
     textBox1.Text = dlgOpenDir.SelectedPath; 
     fileSystemWatcher1.EnableRaisingEvents = true; // Begin watching 
    } 
} 

http://msdn.microsoft.com/en-us/library/x7t1d0ky.aspx

0

경로는 아마 잘못된 문자가 포함되어 있습니다. MSDN documentation on FileInfo constructor

당신이 try 블록에서 경로를 통과 코드를 넣어보기 등에 대한 Try catch Blocks

private void button2_Click(object sender, EventArgs e) 
{ 
    try 
    { 
     fileSystemWatcher1.IncludeSubdirectories = true; 
     DialogResult resDialog = dlgOpenDir.ShowDialog(); 
     if (resDialog.ToString() == "OK") 
     { 
      fileSystemWatcher1.Path = dlgOpenDir.SelectedPath; 
      textBox1.Text = dlgOpenDir.SelectedPath; 
     } 
    } 
    catch(Exception ex) 
    { 

    } 
} 
+0

그래, "ArgumentException 처리되지 않았습니다"그래서 그는 경로가 없다고 말하고있다. 어느 것이 정상이지만 그 오류를주지 않기를 바랍니다. 내 파일 공유자는 즉시보기 시작하지만 처음에는 선택한 경로가 없어야합니다. – Loko

+0

try catch 블록에 넣어두면 예외를 처리하고 처음으로 경로를 선택하지 않으면 응용 프로그램이 중단되지 않습니다. –

+0

정확히 의미합니까? 코드 예제로 보낼 수 있습니까? (방금 C#으로 일주일 전부터 시작했습니다.) – Loko

관련 문제