2012-05-11 3 views
0

私は、オンライン画像ギャラリー(imgur)からデスクトップの壁紙を自動的に変更するための小さなプログラムを作成しています。それはときに最初の実行で正常に動作しますが、2番目の実行ではIOExceptionを返しファイルがまだストリーム配信されているかどうかを確認するには?

namespace Imgur_Wallpapers 
{ 
    public partial class Form1 : Form 
    { 
     //IMGUR API = 3f1e4339e7bad35ff801bf76e369ae17 

     private int Count = 1; 

     public Form1() 
     { 
      InitializeComponent(); 
      timer1.Enabled = false; 
      if (!File.Exists("image_black")) 
      { 
       Bitmap black = new Bitmap(10, 10); 
       black.Save("transitionpaper.bmp"); 
      } 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 

     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      DownloadFromImgur download = new DownloadFromImgur("gjZEc", Path.GetDirectoryName(Application.StartupPath)); 
      Wallpaper.Set(new Uri(Application.StartupPath + "/Image.bmp"), Wallpaper.Style.Centered); 
      Count++; 
     } 

     private void timer1_Tick(object sender, EventArgs e) 
     { 
      DownloadFromImgur download = new DownloadFromImgur(imgurAlbumIdBox.Text, Path.GetDirectoryName(Application.StartupPath)); 

      Wallpaper.Set(new Uri(Application.StartupPath + "/Image.bmp"), Wallpaper.Style.Centered); 
      Count++; 
     } 

     private void button2_Click(object sender, EventArgs e) 
     { 
      timer1.Interval = (int)whenToRefreshBox.Value; 
      timer1.Enabled = true; 
     } 
    } 

     private string url; 
     public List<string> hash; 
     private Random random; 
     private string albumID; 


     public DownloadFromImgur(string albumID, string folderToSaveTo) 
     { 
      try 
      { 
       this.albumID = albumID; 
       hash = new List<string>(); 
       random = new Random(); 
       GetWebSite(); 
       Wallpaper.Set(new Uri(Application.StartupPath + "/transitionpaper.bmp"), Wallpaper.Style.Centered); 
       WebClient client = new WebClient(); 
       File.Delete(Application.StartupPath + "/Image.bmp"); 
       client.DownloadFile(url, Application.StartupPath + "/Image.bmp"); 
       client.Dispose(); 
      } 
      catch (WebException ex) 
      { 

      } 
     } 

     private void GetWebSite() 
     { 
      var doc = XDocument.Load("http://api.imgur.com/2/album/" + albumID); 

      hash.Clear(); 
      RecursiveWrite(doc.Root); 

      url = hash[random.Next(hash.Count - 1)]; 
     } 

     private void RecursiveWrite(XElement node) 
     { 
      foreach (var attribute in node.Value) 
      { 
       if (node.Name == "original") 
       { 
        hash.Add(node.Value); 
        break; 
       } 
      } 

      foreach (var child in node.Elements()) 
      { 
       RecursiveWrite(child); 
      } 
     } 
    } 

    public sealed class Wallpaper 
    { 
     Wallpaper() { } 

     const int SPI_SETDESKWALLPAPER = 20; 
     const int SPIF_UPDATEINIFILE = 0x01; 
     const int SPIF_SENDWININICHANGE = 0x02; 

     [DllImport("user32.dll", CharSet = CharSet.Auto)] 
     static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni); 

     public enum Style : int 
     { 
      Tiled, 
      Centered, 
      Stretched 
     } 

     public static void Set(Uri uri, Style style) 
     { 
      System.IO.Stream s = new System.Net.WebClient().OpenRead(uri.ToString()); 

      System.Drawing.Image img = System.Drawing.Image.FromStream(s); 
      string tempPath = Path.Combine(Path.GetTempPath(), "wallpaper.bmp"); 
      img.Save(tempPath, System.Drawing.Imaging.ImageFormat.Bmp); 

      RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Control Panel\Desktop", true); 
      if (style == Style.Stretched) 
      { 
       key.SetValue(@"WallpaperStyle", 2.ToString()); 
       key.SetValue(@"TileWallpaper", 0.ToString()); 
      } 

      if (style == Style.Centered) 
      { 
       key.SetValue(@"WallpaperStyle", 1.ToString()); 
       key.SetValue(@"TileWallpaper", 0.ToString()); 
      } 

      if (style == Style.Tiled) 
      { 
       key.SetValue(@"WallpaperStyle", 1.ToString()); 
       key.SetValue(@"TileWallpaper", 1.ToString()); 
      } 

      SystemParametersInfo(SPI_SETDESKWALLPAPER, 
       0, 
       tempPath, 
       SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE); 
     } 
    } 
} 

:現在、私はこのコードを持っています。ファイルはまだどこかで使用されているようです。それがまだ使われている場所を見つけるにはどうすればいいですか?ここでは例外メッセージです:

System.IO.IOExceptionが未処理
メッセージだったが=処理することができません アクセスファイル「C:\ Users \ユーザーデビッド\ドキュメントは、Visual Studioを\ 2010プロジェクト\ Imgur壁紙\ Imgur \ Wallpapers \ bin \ Debug \ Image.bmp ' 他のプロセスで使用されているためです。
ソース= mscorlib
のStackTrace:System.IO.File.DeleteでSystem.IO .__ Error.WinIOError(のInt32 errorCodeを、文字列 maybeFullPath)(文字列のパス)

+0

一時ディレクトリは使用しませんが、実行中のフォルダも使用します。巨大な問題! –

+0

@ColeJohnsonコメントを誤解していますか、またはWindowsフォームを使用してOPを侮辱しましたか? –

+3

@ColeJohnsonそれは一つの意見です。しかし、この質問には全く関係ありません。 –

答えて

1

であなたはダウンロードを閉じません完了したらストリーム。そうしないと、ストリームは引き続きファイルにフックされます。 .Close()と.Dispose()を使用してみてください

+0

または「使用中」ブロック – Bridge

+1

ブリー!私はブロックを使用して嫌い。あなたが望むようにしてください。 –

+0

ブロックを使用して@Bridgeを使用しても、.Close()は呼び出されません。そのため、非効率なコードを持つn00bsが多数表示されています –

1

私はいつもこのファイルがどのファイルをロックしていたのか把握するのに非常に役に立ちましたが、このtoolが見つかりました。

私はしばらくは使用していませんが、ショットに値するです。

+0

彼はおそらくファイルをロックする彼のプログラムです:) –

+0

は完全にそれに同意します。 –

関連する問題