Click here to Skip to main content
15,867,879 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
this is my code and i get an error that the filename/ directory is incorrect. The file does exist in that directory. I want to upload this file to my azure blob storage.

C#
private static void UploadFileToBlobStorage()
    {
        var localFilePath = "C:\\Users\\LK\\source\repos\dsd-ica-perf\\src\\et.ure.ica.Perf\\PerfTest.cs";
        BlobServiceClient blobServiceClient = new BlobServiceClient(storageConnStr);

        BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient("perfTest");

        Console.WriteLine("Uploading to Blob storage as blob:\n\t {0}\n", blobServiceClient.Uri);

        BlobClient blobClient = containerClient.GetBlobClient("PerfTest");

        using FileStream uploadingFileStream = File.OpenRead(localFilePath);

        blobClient.Upload(uploadingFileStream);
        uploadingFileStream.Close();
    }


What I have tried:

I tried changing up the localFilePath in different ways also googled but nothing seemed to resolve the issue.
Posted
Updated 30-Aug-22 23:04pm

1 solution

The issue here is with your string containing the path to the file:
"C:\\Users\\LK\\source\repos\dsd-ica-perf\\src\\et.ure.ica.Perf\\PerfTest.cs";

Notice that you've escaped some backslashes, but not others. You need to escape all backslashes otherwise C# will convert those into characters (ie. \r gets converted into a carriage-return):
// Either escape all explicitly
"C:\\Users\\LK\\source\\repos\\dsd-ica-perf\\src\\et.ure.ica.Perf\\PerfTest.cs";

// Or escape using the @ symbol
@"C:\Users\LK\source\repos\dsd-ica-perf\src\et.ure.ica.Perf\PerfTest.cs";
 
Share this answer
 
Comments
Govancekaran 31-Aug-22 5:26am    
I tried using both strings and now i get the error that the specified resource name contains invalid characters
Chris Copeland 31-Aug-22 5:36am    
That sounds like your code isn't able to find the correct container. See this StackOverflow question[^] which seems to explain the problem.
Govancekaran 31-Aug-22 5:48am    
i figured it out thank you this really helped
Chris Copeland 31-Aug-22 5:55am    
You're welcome :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900