Click here to Skip to main content
15,894,291 members
Home / Discussions / C#
   

C#

 
AnswerRe: Transfer data between MFC Application and c# application using NamedPipe Pin
JudyL_MD13-Oct-23 5:05
JudyL_MD13-Oct-23 5:05 
QuestionCreating a JSON File from a CSV file in C# Pin
Member 1611347712-Oct-23 2:19
Member 1611347712-Oct-23 2:19 
AnswerRe: Creating a JSON File from a CSV file in C# Pin
OriginalGriff12-Oct-23 2:29
mveOriginalGriff12-Oct-23 2:29 
AnswerRe: Creating a JSON File from a CSV file in C# Pin
jschell12-Oct-23 5:17
jschell12-Oct-23 5:17 
AnswerRe: Creating a JSON File from a CSV file in C# Pin
Gerry Schmitz12-Oct-23 8:36
mveGerry Schmitz12-Oct-23 8:36 
GeneralRe: Creating a JSON File from a CSV file in C# Pin
OriginalGriff12-Oct-23 9:07
mveOriginalGriff12-Oct-23 9:07 
GeneralRe: Creating a JSON File from a CSV file in C# Pin
Gerry Schmitz12-Oct-23 9:28
mveGerry Schmitz12-Oct-23 9:28 
GeneralRe: Creating a JSON File from a CSV file in C# Pin
OriginalGriff12-Oct-23 10:20
mveOriginalGriff12-Oct-23 10:20 
GeneralRe: Creating a JSON File from a CSV file in C# Pin
jschell13-Oct-23 7:09
jschell13-Oct-23 7:09 
QuestionItemObservableCollection Pin
rex648-Oct-23 7:34
rex648-Oct-23 7:34 
AnswerRe: ItemObservableCollection Pin
Gerry Schmitz8-Oct-23 7:55
mveGerry Schmitz8-Oct-23 7:55 
QuestionHow do I use Visual Studio user secrets and GitHub secrets, in the same project? Pin
Rod at work4-Oct-23 11:32
Rod at work4-Oct-23 11:32 
AnswerRe: How do I use Visual Studio user secrets and GitHub secrets, in the same project? Pin
Richard MacCutchan4-Oct-23 20:55
mveRichard MacCutchan4-Oct-23 20:55 
GeneralRe: How do I use Visual Studio user secrets and GitHub secrets, in the same project? Pin
Rod at work5-Oct-23 2:38
Rod at work5-Oct-23 2:38 
GeneralRe: How do I use Visual Studio user secrets and GitHub secrets, in the same project? Pin
Richard MacCutchan5-Oct-23 2:43
mveRichard MacCutchan5-Oct-23 2:43 
GeneralRe: How do I use Visual Studio user secrets and GitHub secrets, in the same project? Pin
Rod at work5-Oct-23 3:24
Rod at work5-Oct-23 3:24 
GeneralRe: How do I use Visual Studio user secrets and GitHub secrets, in the same project? Pin
Rod at work5-Oct-23 3:54
Rod at work5-Oct-23 3:54 
GeneralRe: How do I use Visual Studio user secrets and GitHub secrets, in the same project? Pin
Richard MacCutchan5-Oct-23 4:18
mveRichard MacCutchan5-Oct-23 4:18 
AnswerRe: How do I use Visual Studio user secrets and GitHub secrets, in the same project? Pin
jschell5-Oct-23 5:48
jschell5-Oct-23 5:48 
GeneralRe: How do I use Visual Studio user secrets and GitHub secrets, in the same project? Pin
Rod at work5-Oct-23 10:33
Rod at work5-Oct-23 10:33 
GeneralRe: How do I use Visual Studio user secrets and GitHub secrets, in the same project? Pin
jschell6-Oct-23 7:10
jschell6-Oct-23 7:10 
QuestionBeginner: Return properties from another Class Pin
Member 159530273-Oct-23 23:58
Member 159530273-Oct-23 23:58 
AnswerRe: Beginner: Return properties from another Class Pin
Gerry Schmitz4-Oct-23 4:32
mveGerry Schmitz4-Oct-23 4:32 
AnswerRe: Beginner: Return properties from another Class Pin
Dave Kreskowiak4-Oct-23 5:36
mveDave Kreskowiak4-Oct-23 5:36 
QuestionUsing await/async Correctly Pin
Kevin Marois25-Sep-23 12:30
professionalKevin Marois25-Sep-23 12:30 
I'm a bit confused on something.

I have a Sharepoint class with a DownloadFileAsync method:
using System.IO;
using System.Threading.Tasks;

namespace WpfApp1
{
    internal class Sharepoint
    {
        public async Task DownloadFileAsync(string folderName, string downloadPath = null)
        {
            var url = _clientContext.Web.ServerRelativeUrl + "/Shared%20Documents/" + folderName;
            Folder targetFolder = _clientContext.Web.GetFolderByServerRelativeUrl(url);
            _clientContext.Load(targetFolder.Files);

            await _clientContext.ExecuteQueryAsync();

            foreach (var file in targetFolder.Files)
            {
                FileInformation sharepointFile = Microsoft.SharePoint.Client.File.OpenBinaryDirect(_clientContext, file.ServerRelativeUrl);
                await _clientContext.ExecuteQueryAsync();

                var path = string.IsNullOrEmpty(downloadPath) ? DEFAULT_DIRECTORY : downloadPath;
                path = path + @"\" + file.Name;
                using (FileStream filestream = new FileStream(path, FileMode.Create))
                {
                    sharepointFile.Stream.CopyTo(filestream);
                }
            }
        }
    }
}

It is called from a Repository class
using System.IO;
using System.Threading.Tasks;

namespace WpfApp1
{
    internal class Repository
    {
        string _appPath;
        Sharepoint _sharePoint;

        public Repository()
        {
            _sharePoint = new Sharepoint();
            _appPath = $"{Path.GetTempPath()}RDSPaymentProcessing";

        }

        public async Task<MyData> GETDataAsync()
        {
            var results = new MyData();

            // If the local file already exists, delete it
            var fileName = $"{Path.GetTempPath()}{LOCAL_APP_DATA_FOLDER}\sharepointdata.xml";
            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }

            // Download all files from the Settings folder into the local user's app path
            await _sharePoint.DownloadFile(SETINGS_FOLDER, _appPath);

            // If the file was downloaded...
            if (File.Exists(fileName))
            {
                // Deserialize it
                results = Serialization.DeSerializeObject<MyData>(fileName);
            }
            else
            {
                //TODO: Replace with custom exception
                throw new FileNotFoundException(fileName);
            }

            return results;
        }
    }
}
and that is called from the MainWindowViewModel's loaded method:
namespace WpfApp1
{
    internal class MainWindowViewModel
    {
        private MyEntity _data;

        /// <summary>
        /// Occurs when the window's Loaded event is callled
        /// </summary>
        public async void WindowLoadedExecuted()
        {
            await _repository.GETDataAsync();
        }
    }
}
The question is about where to use await/async? I want to UI to be responsive throughout all of this? Am I coding this right? Or does await/async only need to be on the Sharepoint class?
In theory, theory and practice are the same. But in practice, they never are.”
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.