나는 온라인 이미지 갤러리 (imgur)에서 바탕 화면 배경 무늬를 자동으로 변경하는 작은 프로그램을 작성 중입니다. 현재이 코드를 가지고 :파일이 여전히 스트리밍되고 있는지 확인하는 방법은 무엇입니까?
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);
}
}
}
첫 번째 실행시에는 정상적으로 작동하지만 두 번째 실행에서는 IOException을 반환합니다. 분명히 파일은 여전히 어딘가에서 사용되고 있습니다. 어떻게 아직 사용 중인지 찾을 수 있습니까? 여기에 예외 메시지입니다 :
System.IO.IOException가 처리되지 않은
메시지했다가 = 공정 수 없습니다 액세스 파일 'C : \ 사용자 \ 데이비드 \ 문서 비주얼 스튜디오를 \ 2010 프로젝트 \ Imgur 배경 화면 \ Imgur \ 다른 프로세스에서 사용 중이기 때문에 Wallpapers \ bin \ Debug \ Image.bmp ' 입니다.
출처 = mscorlib에
스택 트레이스 : System.IO.File.Delete에서 System.IO .__ Error.WinIOError (INT32의 errorCode, 문자열 maybeFullPath) (문자열 경로)
또한 임시 디렉터리를 사용하지 않고 실행중인 폴더를 사용합니다. 거대한 문제! –
@ColeJohnson 귀하의 의견을 잘못 이해하고 계시거나 Windows Forms 사용을위한 OP를 모욕하셨습니까? –
@ColeJohnson 그건 한 가지 의견입니다. 그러나이 질문에 전혀 관련이 없습니다. –