예제 #1
0
        public async Task SendMusic(Guid id)
        {
            using (var context = new MusicContext())
            {
                var music = await context.MusicFiles.SingleOrDefaultAsync(x => x.Id == id);

                if (music is null)
                {
                    return;
                }

                using (var client = new TcpClient())
                {
                    client.Connect(endPoint);
                    using (var stream = client.GetStream())
                    {
                        var data = await File.ReadAllBytesAsync(music.MusicPath);

                        var musicFile = new MusicFile {
                            Id = music.Id, SongName = music.SongName, Author = music.Author, AudioFile = data.ToList()
                        };
                        var json     = JsonConvert.SerializeObject(musicFile);
                        var sendData = Encoding.UTF8.GetBytes(json);

                        stream.Write(sendData, 0, sendData.Length);
                    }
                }
            }
        }
예제 #2
0
        public async Task SaveAllSongsToDb()
        {
            var songPaths = Directory.GetFiles(@$ "{Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)}\Pictures)").ToList();

            using (var context = new MusicContext())
            {
                foreach (var music in songPaths)
                {
                    var parts   = Path.GetFileName(music).Split("-");
                    var isExist = await context.MusicFiles.SingleOrDefaultAsync(x => x.MusicPath == music);

                    if (isExist is null)
                    {
                        continue;
                    }

                    context.MusicFiles.Add(new MusicFileDTO
                    {
                        Author    = parts.First(),
                        SongName  = parts.Last(),
                        MusicPath = music,
                    });

                    await context.SaveChangesAsync();
                }
            }
        }