1
I am trying to create a file and save it in a folder created by me with the name "temporario". I created this folder on disk E: and inside my program, on disk C:. Using File.Create(file), error appears
Acess to the path 'E:\temporario' is denied.
Or, in the case of the folder in my program
Access to the path 'C:\Users\srodrigues\Documents\ProjetoPAP\ConversorPDFImage\Main\temporario' is denied.
I’ve researched a number of sites, in English and Portuguese, and it talks all about giving uncheck on the "Read only" option of the folder and I’ve done it, giving my user permission to write in the folder and I still have the same error.
What can I do to save the file in that folder? I prefer to keep it in the program folder.
OBS: I am manipulating PDF files and if I use document.Save(filepath)
of Pdfsharp, saves in both folders without any problem, never had trouble saving files in folder E:\temporario
, but now even that folder is not enough to save the file.
EDIT: I will put my code here - now complete (with the amendment proposed by Augusto Vasques):
int indexi = 0;
private void proc2_Click(object sender, EventArgs e)
{
openFileDialog1.Title = "Procurar arquivos no computador";
openFileDialog1.InitialDirectory = @"E:\";
openFileDialog1.Filter = "Imagens (.bmp,.jpg,.png,.tiff,.tif) |*.bmp;*.jpg;*.png;*tiff;*tif|Todos os arquivos (*.*)|*.*";
DialogResult resposta = openFileDialog1.ShowDialog();
if (resposta == DialogResult.OK)
{
foreach (string file in openFileDialog1.FileNames)
{
string caminhoCompleto = file;
caminho2 = openFileDialog1.SafeFileName;
caminhotb2.Text = caminhoCompleto;
string fish = "";
string path = @"E:\\temporario";
using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
{
if (Directory.Exists(path))
{
fish = Path.Combine(path, caminho2);
}
else
{
Directory.CreateDirectory(path);
fish = Path.Combine(path, caminho2);
}
File.Create(fish);
}
indexi = listaimg.Items.Count;
listaimg.Items.Add((indexi + 1) + "- " + caminho2);
}
}
}
This is probably because you are trying to create a directory using a resource used to create files. Try using a adequate recourse and see if things work out as expected. Or, if I got it wrong: because you are trying to create a file, but you are not mentioning what this file is, as you yourself highlighted in the address above. The address is written only until the file "temporario" and is interrupted there. This is an error. It is necessary to specify the file
– Caio de Paula Silva
Thanks for the comment Caio, but I do not want to create a directory but a file inside the folder. The problem is, when I try to do it, it makes a mistake.
– Sofia Rodrigues
Try to save this file with an administrator user. Or sometimes it may even be a Windows related problem. Delete the folder and create another one with the same name.
– Pedro Duca
@Caio de Paula Silva I put my code, take a look
– Sofia Rodrigues
@Pedroduca I deleted the folder and created it again, but I still have the same mistake. I am a network user but they put some administrator permissions for me to use, and I am running Visual Studio as Administrator.
– Sofia Rodrigues
Sofia, you guarantee that in addition to the path you are also specifying the file name to be created?
– Caio de Paula Silva
This temporary file is a PDF, right?
– Pedro Duca
@Pedroduca No, the temporary file is an image (it can be PNG, JPG, BMP or TIFF) and then convert to PDF. When I gave the PDF example, it is because I also want to convert PDF to image, so I save PDF files in a temporary folder.
– Sofia Rodrigues
E:\
is a unit on your machine or a network mapping?– Leandro Angelo
E: is the unit of the machine
– Sofia Rodrigues