Пример #1
0
        private async Task CommandListAsync(string parameter)
        {
            if (!authenticated)
            {
                await ReplyAsync(FtpReplyCode.NotLoggedIn, "You need to log in first");

                return;
            }
            MemoryStream stream = new MemoryStream();
            var          writer = new StreamWriter(stream, encoding);

            writer.NewLine = "\r\n";
            try
            {
                var listing = await fileProvider.GetListingAsync(parameter);

                await writer.WriteLineAsync();

                foreach (var item in listing)
                {
                    if (listFormat == ListFormat.Unix)
                    {
                        await writer.WriteLineAsync(
                            string.Format(
                                "{0}{1}{1}{1}   1 owner   group {2,15} {3} {4}",
                                item.IsDirectory ? 'd' : '-',
                                item.IsReadOnly ? "r-x" : "rwx",
                                item.Length,
                                item.LastWriteTime.ToString(
                                    item.LastWriteTime.Year == DateTime.Now.Year ?
                                    "MMM dd HH:mm" : "MMM dd  yyyy", CultureInfo.InvariantCulture),
                                item.Name));
                    }
                    else if (listFormat == ListFormat.MsDos)
                    {
                        if (item.IsDirectory)
                        {
                            await writer.WriteLineAsync(
                                string.Format(
                                    CultureInfo.InvariantCulture,
                                    "{0:MM-dd-yy  hh:mmtt} {1,20} {2}",
                                    item.LastWriteTime,
                                    item.Length,
                                    item.Name));
                        }
                        else
                        {
                            await writer.WriteLineAsync(
                                string.Format(
                                    CultureInfo.InvariantCulture,
                                    "{0:MM-dd-yy  hh:mmtt}       {1,-14} {2}",
                                    item.LastWriteTime,
                                    "<DIR>",
                                    item.Name));
                        }
                    }
                    else
                    {
                        throw new NotSupportedException("Can't only use Unix or MS-DOS listing format.");
                    }
                }
            }
            catch (FileBusyException ex)
            {
                await ReplyAsync(FtpReplyCode.FileBusy, string.Format("File temporarily unavailable: {0}", ex.Message));

                return;
            }
            catch (FileNoAccessException ex)
            {
                await ReplyAsync(FtpReplyCode.FileNoAccess, string.Format("File access denied: {0}", ex.Message));

                return;
            }
            writer.Flush();
            stream.Seek(0, SeekOrigin.Begin);
            await OpenDataConnectionAsync();

            await dataConnection.SendAsync(stream);

            await dataConnection.DisconnectAsync();

            await ReplyAsync(FtpReplyCode.SuccessClosingDataConnection, "Listing has been sent");

            return;
        }