private void SaveGifs(List <string> filePaths) { if (filePaths.Count == 0) { return; } //Make this async and not on the main UI thread using (var gifContext = new GifContext()) { filePaths.ForEach(file => { try { lstFiles.Items.RemoveByKey(file); Gif gif = gifDictionary[Path.GetFileName(file)]; gifContext.Gifs.Add(gif); foreach (var giftag in gif.GifTags) { gifContext.Entry(giftag.Tag).State = System.Data.Entity.EntityState.Unchanged; } File.Copy(file, Path.Combine(Gif.TargetDirectory, Path.GetFileName(file))); } catch (IOException ioException) { } catch (Exception exception) { } }); gifContext.SaveChanges(); // Save changes to DB } MessageBox.Show("Saved " + filePaths.Count + " gifs"); filePaths.Clear(); gifDictionary.Clear(); }
public async Task GifAsync([Summary("Gets Specific Gif By Identifier Number")] int num = -1) { var watch = new System.Diagnostics.Stopwatch(); string comment = ""; using (var gifContext = new GifContext()) { if (num == -1) { //Get random gif Random rand = new Random(); num = rand.Next(0, gifContext.Gifs.Count()); comment = "random "; } Gif gif = gifContext.Gifs.First(g => g.Identifier == num); if (gif == null) { await Context.Channel.SendMessageAsync("Gif Not Found"); return; } string fileName = gif.FileName; string fullPath = Gif.TargetDirectory + '\\' + fileName; gif.DisplayCount++; watch.Start(); await gifContext.SaveChangesAsync(); watch.Stop(); await Context.Channel.SendFileAsync(fullPath, "Sending " + comment + "gif #" + num.ToString() + " took " + watch.ElapsedMilliseconds.ToString() + " ms to save"); } }
private void btnBrowse_Click(object sender, EventArgs e) { OpenFileDialog fileDialog = new OpenFileDialog(); fileDialog.Multiselect = true; fileDialog.Title = "Select Gifs to Add"; fileDialog.Filter = "Gifs (*.GIF)|*.GIF|" + "All files (*.*)|*.*"; DialogResult dr = fileDialog.ShowDialog(); if (dr == DialogResult.OK) { using (var gifContext = new GifContext()) { List <Gif> gifs = gifContext.Gifs.ToList <Gif>(); // Read the files foreach (String file in fileDialog.FileNames) { // Create a PictureBox. try { if (!FilePaths.Contains(file)) { if (!gifs.Any(gif => gif.FileName == Path.GetFileName(file))) { Gif gif = new Gif { FileName = Path.GetFileName(file), CreatorId = User.ManagerId }; lstFiles.Items.Add(file, file, 0); FilePaths.Add(file); gifDictionary[Path.GetFileName(file)] = gif; } else { if (!DuplicateFilePaths.Contains(file)) { DuplicateFilePaths.Add(file); lstDuplicate.Items.Add(file); } } } } catch (SecurityException ex) { // The user lacks appropriate permissions to read files, discover paths, etc. MessageBox.Show("Security error. Please contact your administrator for details.\n\n" + "Error message: " + ex.Message + "\n\n" + "Details (send to Support):\n\n" + ex.StackTrace ); } catch (Exception ex) { // Could not load the image - probably related to Windows file system permissions. MessageBox.Show("Cannot add the image: " + file.Substring(file.LastIndexOf('\\')) + ". You may not have permission to read the file, or " + "it may be corrupt.\n\nReported error: " + ex.Message); } } } } }
private void button1_Click(object sender, EventArgs e) { //var optionsBuilder = new DbContextOptionsBuilder<GifContext>(); //optionsBuilder.UseSqlServer(ConfigurationManager.ConnectionStrings["GifModel"].ConnectionString); //using (var gifContext = new GifContext(optionsBuilder.Options)) { using (var gifContext = new GifContext()) { var user = new User { Id = new Guid(), Username = "******", Usertag = "4948" }; gifContext.Users.Add(user); // Add user gifContext.SaveChanges(); // Save changes to DB } }
private void btnAddTag_Click(object sender, EventArgs e) { string input = Microsoft.VisualBasic.Interaction.InputBox("Enter new tag name", "Add Tag", "Default", 600, 600).Trim(); if (input.Length > 0) { using (var gifContext = new GifContext()) { List <Tag> tags = gifContext.Tags.ToList <Tag>(); if (!tags.Any(tag => tag.Name == input)) { gifContext.Tags.Add(new Tag { Name = input, CreatorId = User.ManagerId }); gifContext.SaveChanges(); // Save changes to DB } RefreshTags(null); } } }
private List <Tag> GetTags() { using (var gifContext = new GifContext()) { return(gifContext.Tags.ToList <Tag>()); } }
private async Task QuickAddAsync(IProgress <BaseProgressReportModel> progress, CancellationToken cancellationToken) { int completed = 0; int numNewGifs = 0; int totalGifs = 0; progress.Report(new BaseProgressReportModel() { MainTask = "Quick Add Gifs", CurrentTask = "Starting", PercentageComplete = 0, SpecialInfo = "Total New Gifs: " + numNewGifs.ToString(), CanCancel = true }); using (var gifcontext = new GifContext()) { var gifHashSet = new HashSet <string>(gifcontext.Gifs.Select(gif => gif.FileName)); string[] files = Directory.GetFiles(Gif.TargetDirectory); totalGifs = files.Length; foreach (var file in files) { completed++; if (!gifHashSet.Contains(Path.GetFileName(file))) { gifcontext.Gifs.Add(new Gif() { FileName = Path.GetFileName(file), CreatorId = User.ManagerId }); numNewGifs++; progress.Report(new BaseProgressReportModel() { MainTask = "Quick Add Gifs", CurrentTask = "Added " + file, SpecialInfo = "Total New Gifs: " + numNewGifs.ToString(), PercentageComplete = (completed * 100 / (totalGifs + 10)) });; } else { progress.Report(new BaseProgressReportModel() { MainTask = "Quick Add Gifs", CurrentTask = "Scanned Duplicate " + file, SpecialInfo = "Total New Gifs: " + numNewGifs.ToString(), PercentageComplete = (completed * 100 / (totalGifs + 10)) }); } cancellationToken.ThrowIfCancellationRequested(); } if (numNewGifs > 0) { progress.Report(new BaseProgressReportModel() { MainTask = "Quick Add Gifs", CurrentTask = "Saving Changes to Database...", PercentageComplete = (completed * 100 / (totalGifs + 10)), SpecialInfo = "Total New Gifs: " + numNewGifs.ToString(), CanCancel = false });; await Task.Delay(1000); await gifcontext.SaveChangesAsync(); } progress.Report(new BaseProgressReportModel() { MainTask = "Quick Add Gifs", CurrentTask = String.Format("Added {0} new gifs for a total of {1} gifs", numNewGifs, totalGifs), PercentageComplete = 100, SpecialInfo = "Total New Gifs: " + numNewGifs.ToString(), CanCancel = false }); } }