Unauthorized access trying to save .rtf file [closed] - c#

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I'm having an issue in saving an rtf file, I'm saving it in a subfolder in "MyDocuments" the strange this is that i also save a .bin file and some other stuff and it doesn't causes any exeption, i'am pc admin so why does it happen?
string docs = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\Monitor\\Immobili\\";`
richTextBox1.SaveFile(docs+code);
code is also another string

An UnauthorizedAccessException means one of 3 things:
The caller does not have the required permission.
Path is a directory.
Path specified a read-only file.
Check for last two reasons. I think you are not adding filename after path. Add breakpoints and check if richTextBox1.SaveFile(docs+code); is getting the complete path to the file instead of just folder path.

Related

C# How do I prevent a file from saving without the asked extension? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I'm currently writing a program and using my own file extension ".pn" for these files. When I use the BinaryWriter in FileMode.Create to write to the file "primes.pn" and then look in the file explorer it does not have the extension I asked. My file explorer has display file extensions turned on. How could I prevent this from happening/what is causing this?
Additionally the check for whether "primes.pn" exists passes but then when checking the length of the file it throws a System.IO.FileNotFoundException.
if (!File.Exists("res_divisibility/primes.pn") && bytes.Count > new FileInfo("res_divisibility/primes.pn").Length)
{
BinaryWriter bw = new BinaryWriter(new FileStream("res_divisibility/primes.pn", FileMode.Create));
foreach(byte b in bytes)
bw.Write(b);
}
Still not sure what was causing the problem but File.WriteAllBytes()solves this issue. The existence check was my mistake because I was using && instead of ||.

Is it possible to build a file in the parent directory? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I have a folder structure in the project as seen in the picture:
and I would like to have the SettingsManagementTest.mvconfig File to be directly in the Release/Debug folder. But it is always built into a subfolder called SettingsManagement.
Is this even possible or should think of another solution?
Call var currentDir = Directory.GetCurrentDirectory(); and you will land in your Debug folder. I recommend printing currentDir out once so you have a better understanding of what the method returns.
You can call var bin = Directory.GetParent(currentDir); to path into your bin folder.
You use var release = Path.Combine(bin, "Release"); to path into your Release folder.
To create your desired file in the Release folder you can call File.Create(Path.Combine(release, "SettingsManagementTest.mvconfig"));

Files not being found [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
My project is not finding files that definitely exist. I am using the Stanford NLP library and was getting file not found exceptions which I began debugging.
Here is my test code:
String jarRoot = #"stanford-corenlp-full-2016-10-31\stanford-corenlp-full-2016-10-31\stanford-corenlp-3.7.0-models\edu\stanford\nlp\models\pos-tagger\english-left3words\";
foreach (String fName in Directory.GetFiles(jarRoot))
{
Console.WriteLine("File in jarRoot: " + fName);
Console.WriteLine("File exists? " + File.Exists(fName));
}
The output:
File in jarRoot: stanford-corenlp-full-2016-10-31\stanford-corenlp-full-2016-10-31\stanford-corenlp-3.7.0-models\edu\stanford\nlp\models\pos-tagger\english-left3words\english-left3words-distsim.tagger
File exists? False
File in jarRoot: stanford-corenlp-full-2016-10-31\stanford-corenlp-full-2016-10-31\stanford-corenlp-3.7.0-models\edu\stanford\nlp\models\pos-tagger\english-left3words\english-left3words-distsim.tagger.props
File exists? False
How could File.Exists() possibly be returning false?
Screenshots of directory:
This was sorted out in the comments of the question. Opening the file using a FileStream threw a 'System.IO.PathTooLongException' exception. File.Exists() simply returns false if it encounters any errors such as file path being too long.
#Abbas provided this link that fixed the issue and may be helpful:
Why does System.IO.File.Exists(string path) return false?
Thanks everyone!

How to give path properly in C# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have a folder in my project named Export, I save files to that folder using this code :
document.Save(#"D:\workspace\folder1\Solution.Application.DataExporter\Export\mydocument.pdf");
But when others use this code, they complain that they don't have that path. How can I give path to my code so that it works everywhere? Thanks.
Option 1: Use the Environment.SpecialFolder to get the physical location of a special folder. See here for an overview of possible special folders.
For example, if you want to put the document in 'my documents' folder, then Environment.SpecialFolder.MyDocuments would give you the location to the my documents folder on the current machine.
Code:
var path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
This way you are sure you always have the correct and existing location. If needed, you can always first create an export folder into this special folder with Directory.CreateDirectory(), if it does not exist yet.
Option 2: Of course, you can always ask for a location to the user if you don't want to use a predefined one, by using the SaveFileDialog class, for example.
Create the folder 'Export' in MyDocuments, and then save the file to that directory. As many others have pointed out. You need to save to a directory, that the executing user has access rights to.
string documentFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), #"Export");
if (!Directory.Exists(documentFolder))
{
Directory.CreateDirectory(documentFolder);
}
document.Save(Path.Combine(documentFolder, "mydocument.pdf"));

FileNotFoundException on file that exists - C# [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Hi
I am trying to read a plain text file named "test.txt" from my system but all the attempts
File.Exists(), StreamReder are not getting the file, that is not a new task for me but i am irritated due to this strange behavior. I have given full permissions to the file but in vain. I am doing the test in a C# console application. The system has a fresh installation and I am wondering of any permission issue when run in debug mode. I also copied the file to Debug folder but still same error. Can anyone please guide me about this? Thanks in advance
There is a neat function in C# for reading string files: (in System.IO namespace)
string text = File.ReadAllText("test.txt");
If you are having trouble with the path, you can add test.txt as a resource with copy to (add the file to teh project, right-click properties and select Copy to output directory.
Then you can use:
string path = Path.Combine(Directory.GetCurrentDirectory(), "test.txt");
File.ReadAllText(path);
Have you stepped through the code? The first step is to make sure the path being used in the program is correct.
Debug mode will still run under your account so, if you have permission to open the file, that won't be a problem.

Categories