Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to find whether a file is still being streamed?
    text
    copied!<p>I am writing a small program to automatically change the desktop wallpaper, from an online image gallery (imgur). Currently I have this code:</p> <pre><code>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&lt;string&gt; hash; private Random random; private string albumID; public DownloadFromImgur(string albumID, string folderToSaveTo) { try { this.albumID = albumID; hash = new List&lt;string&gt;(); 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); } } } </code></pre> <p>It works fine when on the first run, but returns an IOException on the second run. Apparently the file is still being used somewhere. How can I find where it's still used? Here's the exception message:</p> <blockquote> <p>System.IO.IOException was unhandled<br> Message=The process cannot access the file 'C:\Users\David\documents\visual studio 2010\Projects\Imgur Wallpapers\Imgur Wallpapers\bin\Debug\Image.bmp' because it is being used by another process.<br> Source=mscorlib<br> StackTrace: at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.File.Delete(String path)</p> </blockquote>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload