AddFontFile error: file not found - c#

I followed this answer for my project, but failed to start because of the file not found error.
A first chance exception of type 'System.IO.FileNotFoundException' occurred in System.Drawing.dll
Additional information: File not found.
Here is my code:
PrivateFontCollection modernFont = new PrivateFontCollection();
modernFont.AddFontFile("digital.ttf");
label5.Font = new Font(modernFont.Families[0], 40);
The font file is located right in the namespace directory, is there something wrong with the the file path or else?

Related

Upload file using webclient: exception file not found

I am trying to upload an excel file using webclient:
using (var client = new WebClient()) {
client.Credentials = new NetworkCredential(myname, mypassword);
byte[] responseArray = client.UploadFile(
newUri("https://www.syros.nl/prive/excel/newexcelfile.xlsx";), "POST", #"C:/Temp/~myfile.xslx");
}
Whatever I try, I keep getting an webexception:
Exception thrown: 'System.Net.WebException' in System.dll An unhandled exception of type System.Net.WebException' occurred in System.dll An exception occurred during a WebClient request.
and an inner exception:
FileNotFoundException: Could not find file 'C:\Temp\~myfile.xslx
I googled this extensively, but no one seems to have this problem.
Can someone suggest what I am doing wrong? The file is definitely present.
I tried different locations for the file, different names (with/without ~), even other/wrong credentials.

Lumenworks CsvReader Exception

I need to parse a csv file and import it into a oracle database table. I use the Lumenworks Framework with this code:
using (CsvReader csv = new CsvReader(new StreamReader(sFile), true))
{
Console.WriteLine("test3");
}
But if I run the code, the following exception appears:
Application: Application.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.IO.FileNotFoundException
Stack:
at Application.Program.Main(System.String[])
But the weird thing is, if I only execute the new Streamreader(sFile) part and write this on the console, no exception appears. I already debugged the sFile and this is a valid path.
If you have a new StreamReader(sFIle); and the file does not exists it will throw an exception. The path could be a valid formatted path, but if the file is not there then the exception thrown, FileNotFoundException, would make perfect sense.
Check to make sure that the file exists at the specified path before trying to open the stream.
if (File.Exists(sFIle) {
using (CsvReader csv = new CsvReader(new StreamReader(sFile), true)) {
Console.WriteLine("test3");
}
}
What a mistake. After hours I realized that the Lumenworks.dll wasn't copied to the application.exe..
Another exception than System.IO.FileNotFoundException would be truly grateful.

Word2013 not open password file having length > 15 in C#

using Word = Microsoft.Office.Interop.Word;
var app = new Word.Application();
var doc = app.Documents.Open("file.docx", PasswordDocument: "<password>");
I am trying to open word file having password length > 15.
It gives error of invalid command line.
Error Detail:
An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in Application.exe
Additional information: Command failed
Note: I need resolution for word interop i may not allow to user third party.
Same file can open by giving manual password.
Any one have any resolution?

load file from relative path

I am currently doing this:
XDocument feedXml = XDocument.Load("C:/NewsFeed/NewsFeed/App_Data/WorldNews.xml");
But I'd like to use a relative path, so I I've tried the following:
XDocument feedXml = XDocument.Load("~/App_Data/WorldNews.xml");
And set the property, Copty to Output Directory, to "Copy Always".
But I'm getting the following error:
An exception of type 'System.IO.DirectoryNotFoundException' occurred in mscorlib.dll but was not handled in user code
Additional information: A part of the path 'C:\Program Files (x86)\IIS Express\~\App_Data\WorldNews.xml' was not found.
Any help please?
XDocument.Load doesn't know anything about mapping paths. Instead, you should use HttpServerUtility.MapPath to map the path, then pass the result into XDocument.Load:
var path = HttpContext.Current.Server.MapPath("~/App_Data/WorldNews.xml");
var feedXml = XDocument.Load(path);

c# Access to path denied?

I'm having trouble with an error. I have searched the web but havent found an answer that made sense to me. I'm basically trying to create a temporary text file, and write to it. Here it the code concerning the error:
using ( StreamWriter output = new StreamWriter(File.Create(GetTemporaryDirectory())))
and the getTemporaryDirectory method:
public string GetTemporaryDirectory() {
string tempDirectory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
string tempFile = Path.ChangeExtension(tempDirectory, ".txt");
Directory.CreateDirectory(tempFile);
return tempFile;
}
and last but not least the error:
dir = C:\Users\Jack Givens\AppData\Local\Temp\5ftxwy31.txt
A first chance exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll
An unhandled exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll
Additional information: Access to the path 'C:\Users\Jack Givens\AppData\Local\Temp\0lpe1k5t.txt' is denied.
If anyone can tell me what is wrong with my code and what i need to do to fix it, I will appreciate it. side note: sorry for crappy code, i'm kinda a beginner :)
Directory.CreateDirectory(tempFile);
You have just created a directory, the name of which ends in "*.txt".
Then you attempt to create a file with the exact same path. But that's not possible.
You call CreateDirectory on your filename so now a folder exists in the path that File.Create is attempting to call. Just simply remove the Directory.CreateDirectory(tempFile); line (it is not needed as the folder is guaranteed to exist) and your code should work.
You are creating a directory, not a file. You can't open a directory as a file.

Categories