C# how to get directory info with avoiding readonly files - directoryinfo

how can i get directory info without getting readonly file or folders here is my code but this when the file or folder is readonly returns a error!
var dirinfo = new DirectoryInfo(Path_Tb_Path.Text);
var entries = dirinfo.GetFileSystemInfos("**", SearchOption.AllDirectories);

Related

The specified share already exists on Azure Storage File Shares

I am using "Azure Storage File Shares" to store some files from our website, but failed with error message "The specified share already exists".
I have change the file that being upload, but the error persist.
Here my code
public static void Test2Upload()
{
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
string connectionString = "DefaultEndpointsProtocol=https;AccountName=xxxxx;AccountKey=xxxxx;EndpointSuffix=core.windows.net";
string shareName = "myapp-dev";
string dirName = "files";
string fileName = "catto.jpg";
// Path to the local file to upload
string localFilePath = #"d:\temp\two.jpg";
// Get a reference to a share and then create it
ShareClient share = new ShareClient(connectionString, shareName);
share.Create();
// Get a reference to a directory and create it
ShareDirectoryClient directory = share.GetDirectoryClient(dirName);
directory.Create();
// Get a reference to a file and upload it
ShareFileClient file = directory.GetFileClient(fileName);
using (FileStream stream = File.OpenRead(localFilePath))
{
file.Create(stream.Length);
file.UploadRange(
new HttpRange(0, stream.Length),
stream);
}
}
Looks like I should not create ShareClient with same name several times.
Then how to check and use it?
The most important question is, why the file still not yet uploaded (even if I rename the ShareClient object)?
Looks like I should not create ShareClient with same name several
times. Then how to check and use it?
You can use ShareClient.CreateIfNotExists instead of ShareClient.Create method. Former will try to create a share but if a share already exists, then it won't be changed.
You can also use ShareClient.Exists to check if the share exists and then create it using ShareClient.Create if it does not exist. This is not recommended however as it might not work if multiple users are executing that code at the same time. Furthermore, you will be making 2 network calls - first to check the existence of share and then the second to create it.
The most important question is, why the file still not yet uploaded
(even if I rename the ShareClient object)?
Your code for uploading the file looks ok to me. Are you getting any error in that code?
We could use ShareClient.CreateIfNotExists when creating ShareClient object to avoid the problem. Like below
ShareClient share = new ShareClient(connectionString, shareName);
share.CreateIfNotExists();
You might found Similar problem on ShareDirectoryClient.
This part purpose is to create the folder structure.
The upload will fail if the destination folder is not exist.
Error will occur if we create a folder when it already exist.
So, use method ShareDirectoryClient.CreateIfNotExists, like below
ShareDirectoryClient directory = share.GetDirectoryClient(dirName);
directory.CreateIfNotExists();
Here my complete code
public static void TestUpload()
{
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
string connectionString = "DefaultEndpointsProtocol=https;AccountName=xxx;AccountKey=xx;EndpointSuffix=core.windows.net";
string shareName = "myapp-dev";
string dirName = "myfiles";
string fileName = "catto.jpg";
string localFilePath = #"d:\temp\two.jpg";
// Get a reference to a share and then create it
ShareClient share = new ShareClient(connectionString, shareName);
share.CreateIfNotExists();
// Get a reference to a directory and create it
ShareDirectoryClient directory = share.GetDirectoryClient(dirName);
directory.CreateIfNotExists();
// Get a reference to a file and upload it
ShareFileClient file = directory.GetFileClient(fileName);
using (FileStream stream = File.OpenRead(localFilePath))
{
file.Create(stream.Length);
file.UploadRange(
new HttpRange(0, stream.Length),
stream);
}
}

Azure function - can not read variable.json file

I have some variables which are going to be used by the business logic part of a function. Therefore, instead of adding them inside the appsetting.json file, I have added a separated file as variable.json
Testing on my machine works but after deploy, it seems function can not find it. and I got an error:
The properties for this file is like the below image. (The build action was None before, but nothing has been changed even by content)
and the below image shows how it looks like in root
And because of that reason, any call the response will be "Function host is not running."
The code for reading this file (path = "Variables.json")
private static List<Variable> GetVariables(string path)
{
string json = File.ReadAllText(path);
var variables = JsonConvert.DeserializeObject<List<Variable>>(json);
return variables;
}
Does anyone have any clue why this is happening?
Problem was because when we start Azure Function locally the file varibale.json is available by Directory.GetCurrentDirectory(), but published on azure portal it's Directory.GetCurrentDirectory() + #"\site\wwwroot"
To get the correct folder path you can use following code:
public static HttpResponseMessage Run(HttpRequestMessage req, ExecutionContext context)
{
var path = System.IO.Path.Combine(context.FunctionDirectory, "varibale.json");
// ...
}
For startup.cs, you can use the following code:
var executioncontextoptions = builder.Services.BuildServiceProvider()
.GetService<IOptions<ExecutionContextOptions>>().Value;
var currentDirectory = executioncontextoptions.AppDirectory;

Is there a way to open and read zip files programmatically uploaded by PXUploadDialog?

I have a user that wants to upload a zip file that contains multiple images and a single CSV file with data related to the images. They want to be able to upload the zip file and have the program dissect it by finding and processing the data within the CSV file and then storing the images within the zip to their appropriate locations.
I'm trying to figure out how to open the zip so I can cycle through each file in there to find what I need. Is there any way to do this?
You can use ZipArchive from Acumatica Framework:
// Uploaded file needs to be attached to a DAC record
Guid[] files = PXNoteAttribute.GetFileNotes(DACCache, DACRecord);
UploadFileMaintenance upload = PXGraph.CreateInstance<UploadFileMaintenance>();
foreach (Guid fileID in files)
{
FileInfo fileInfo = upload.GetFile(fileID);
if (fileInfo != null)
{
using (MemoryStream stream = new MemoryStream(fileInfo.BinData))
{
ZipArchive zip = ZipArchive.OpenReadonly(stream);
string tempDirectory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
Directory.CreateDirectory(tempDirectory);
ZipFolder.Decompress(zip, tempDirectory, true);
foreach (string filePath in Directory.GetFiles(tempDirectory))
{
// Enumerating decompressed files
}
}
}
}

Relative path for static files in Expressjs or Koajs

I have a Structure for my application similar to below :
ApplicationA
a.js
ApplicationB
b.js
ApplicationC
c.js
public
i have some static file in public directory . how can application a.js that located in ApplicationA directory access to public directory.
var path = require("path")
var target = "../public/"
var resolved = path.relative(__dirname, target)
Do you mean you want to go up a directory? If so use ../ to go up a directory from where you are so for instance if ApplicationA and public are in the same directory you could use:
../public/filename.file

Windows store app not working

I'm creating a windows store app to read write files.
I gave permissions for document library but still getting this error
App manifest declares document library access capability without specifying at least one file type association
The code snippet of my code:
private async void Button_Click_1(object sender, RoutedEventArgs e)
{
String temp = Month.SelectedValue.ToString() + "/" + Day.SelectedValue.ToString() + "/" + Year.SelectedValue.ToString(); //((ComboBoxItem)Month.SelectedItem).Content.ToString();
DateTime date = Convert.ToDateTime(temp);
Windows.Storage.StorageFolder installedLocation = Windows.ApplicationModel.Package.Current.InstalledLocation;
StorageFolder storageFolder = KnownFolders.DocumentsLibrary;
StorageFile sampleFile = await storageFolder.CreateFileAsync("sample.txt");
var buffer = Windows.Security.Cryptography.CryptographicBuffer.ConvertStringToBinary(temp, Windows.Security.Cryptography.BinaryStringEncoding.Utf8);
await Windows.Storage.FileIO.WriteBufferAsync(sampleFile, buffer);
buffer = await Windows.Storage.FileIO.ReadBufferAsync(sampleFile);
}
Any other better approach is also acceptable.
1.I don't have access to Skydrive. 2.Also don't want to use filepicker
You need to specify file type association also.
From: http://msdn.microsoft.com/en-us/library/windows/apps/hh967755.aspx
Documents Library:
Note You must add File Type Associations to your app manifest that declare specific file types that your app can access in this location.
you can find it in the app manifest, when you check documents library in capabilites, you have to fill in atleast one file type association under Declarations tab.
Found exactly what i was looking for. File type associations was to be added in the app.manifest
For those who facing the same problem check this link

Resources