I'm struggling to open a PDF file inside of Unity. Currently, my application will open up the folder location instead of opening the actual PDF itself.
I've tried using both System.Diagnostics.Process.Startand Application.OpenURL but they all act the same.
Right now, my code looks like:
Application.OpenURL(Application.dataPath + "/PDFS/" + pdfFile);
Now when I hard code in the file location like below, it opens up the PDF correctly:
Application.OpenURL("C:\\Users\\user\\Documents\\Locator\\Assets\\PDFS\\foo.pdf");
Normally I'd leave it hard coded, but I need to allow one button to open any PDF. How can I resolve this?
The string output of the two lines below are most likely not equal.
Application.OpenURL(Application.dataPath + "/PDFS/" + pdfFile);
Application.OpenURL("C:\\Users\\user\\Documents\\Locator\\Assets\\PDFS\\foo.pdf");
Ensure the paths are the same and you should get the result you expect.
See the documentation for Application.OpenURL here:
http://docs.unity3d.com/ScriptReference/Application-dataPath.html
If you read to the bottom, you'll notice:
"Note that the string returned on a PC will use a forward slash as a folder separator."
This likely the reason why you're getting different results.
Also note that the value of Application.OpenURL changes based on the platform.
string pdfURL = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + HttpRuntime.AppDomainAppVirtualPath + "Documents/keyboard-shortcuts-Visual-Studio-Code.pdf";
it return below url and run in browser
http://localhost:1727/Documents/keyboard-shortcuts-Visual-Studio-Code.pdf
thanks
upendra
Related
As part of my c# learning, I have a simple game on a console application. I have now added a text file to record the top score. It has 4 lines, date, number of attempts, time in minutes and additional time in seconds. The text file is part of the project solution, and although I don't specify the full path, I can read the file with StreamReader and output the lines without any trouble. But if I have a new top score, and I wish to overwrite the values in the text file, nothing is happening. The text file remains unmodified.
I have tried using StreamWriter. It feels somewhat intuitive to use StreamReader ReadLine to bring the data into the application and StreamReader WriteLine to output. But the output piece is not working. For my sins, this is my code. There are some similar queries on stackooerflow, but I've not found a solution that appears to fit my query.
if (newBestScore== true)
{
DateTime dateToday = DateTime.Now;
string dateString=dateToday.ToShortDateString();
string currAttempts=CurrentAttempts.ToString();
using (StreamWriter newTopScore = new StreamWriter("TopScore.txt"))
{
newTopScore.WriteLine(dateString);
newTopScore.WriteLine(currAttempts);
newTopScore.WriteLine(timeTaken.Minutes);
newTopScore.WriteLine(timeTaken.Seconds);
newTopScore.Close();
}
}
I'll not fix your problem, but I'll tell you how you can fix all problems of this sort by yourself in the future.
Download Process Monitor and add a filter for your application, like so:
Process Name, contains, <myapp>.exe then Include and then add this filter by pressing the Add button.
Then start recording and look for TopScore.txt and see in which directory it looks for that file. You can use the find dialog for that (Ctrl+F):
Then have a look at the full file name of that file, so you know where it it being written. You can even right click and choose "Jump to ...".
Understand what a working directory is. The file will be written in the working directory if you didn't provide a full path.
Why go this way? This way proves that you have a problem independently. You don't change the source code. Whenever you change the source code and e.g. add diagnostics code like Console.WriteLine(Path.GetFullPath(...));, problems may disappear.
I'm making a application for steam games. Because shortcuts steal zone from desktop ,so i make this project for this. I want add a "drop file here to add list" when he put to shortcut on program i need get the app id, then it will automatically add to list.I added the drop file script!! But i dont know how to get URL title from assembly..There is a screenshot. I want get url from shortcut like this.
SCREENSHOT 1
SCREENSHOT 2
!! I made the drop file script.. I just have problems at get url script.
Opening steam applications is very simple, if you know the location of your steam.exe.
System.Diagnostics.Process.Start(#"C:\Program Files (x86)\Steam\steam.exe","steam://rungameid/730");
In my case (i think its default as well) you can start an application just by giving the path of the steam.exe and passing the content of the shortcut as argument.
Edit:
If you want to get the argument value you can just read the File using
File.ReadAllLines(path);
And get the argument from the string, which will then look like this:
string[] lines = File.ReadAllLines(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "/Golf With Your Friends.url");
string argument = lines.ToList().FirstOrDefault(item => { return item.Contains("steam://"); }).Replace("URL=", "");
System.Diagnostics.Process.Start(#"C:\Program Files (x86)\Steam\steam.exe", argument);
i currently have a word doc. file merging program and am attempting to open a specific word doc. depending on user selection.
// sample of code used:
string outputFolder = null;
...
// file selection
...
string outcomeFolder = outputFolder;
string outputFile = "Combined Folder " + fileDate + " # " + fileTime + ".docx";
string outputFileName = Path.Combine(outcomeFolder, outputFile);
in the program, outputFolder is selected by the user via a fileBrowserDialog
currently, the program runs correctly and merges the files in the folder selected by the user however it fails to open Microsoft Word as well as the outcome document merged.
i've attempted to use:
Microsoft.Office.Interop.Word.Application officeApp =
new Microsoft.Office.Interop.Word.Application();
...
// merging code
...
Document documentTest = officeApp.Documents.Open(outputFileName);
i've noticed that though the program fails to launch Word, Task Manager continues to create a new instance of Word. The merged document formed also cannot be deleted as it claims the file is currently in use. It's as if program it's opening in the background however not physically launching. The Task Manager instance of Word must then be killed before the merged file can be edited / deleted
Any suggestions as to remedy this? Am i missing something simplistic or is the issue due to the non-static file path? - if any additional information is required please ask. thank you
Update 1:
Since implementing the officeApp.Visible = true; the program now launches the file created which can then be edited / re-saved etc. However if i immediately run the program again, attempting to create another merged file within the same folder etc. I am presented with "RPC server is unavailable. (Exception from HRESULT: 0x800706BA)"
Update 2:
As listed above, I was getting a generic HRESULT error code which I have now since remedied. I moved the "new officeApp" into the "Merge" handler which seems to be allowing multiple merges in quick succession without throwing errors.
Update 3:
To make things more simplistic, i've experimented with implementing Process.Start(outputFileName); to open the document. This is due to the additional check box I am now introducing which allows the user to decide whether the merged doc. will be launched / presented once created. This new code also prevents the additional Word.exe's from being created if the file visibility is set to false.
thank you all for your suggestions and help.
Have you tried making Word visible?
officeApp.Visible = true;
My App creates a Content.pdf file that links to Server.pdf and Client.pdf. This is the folder structure:
..\MyApp\Ressources\Content.pdf
..\MyApp\Ressources\Server\Server.pdf
..\MyApp\Ressources\Client\Client.pdf
Furthermore, the folders and files get burned on a CD/DVD or get backed up on a network drive - that causes me to use relative file paths. I use following code to create the Content.pdf:
relative WebLink:
var relativeFileLinkPath = "./" + Directory.GetParent(doc.Uri.LocalPath).Name + "/"+ doc.OutputFileName;
page.AddWebLink(pdfrect, relativeFileLinkPath);
relative FileLink:
var relativeFileLinkPath = "./" + Directory.GetParent(doc.Uri.LocalPath).Name + "/"+ doc.OutputFileName;
page.AddFileLink(pdfrect, relativeFileLinkPath);
Both work fine on local drive, but my issues are:
WebLinks do not work/open on network drive
FileLinks close the Content.pdf and replace it with the linked pdf file
Questions:
How can I modify the FileLinks to get opened in a new (PDF reader) instance/window?
Holding the CTRL-Key when clicking on the FileLink is an option but not a nice solution.
What’s the right syntax for relative PDF web links?
AddFileLink is the correct method for files sitting side by side in a folder.
To control whether a new window will be opened, a small modification of PDFsharp's PdfLinkAnnotation class will be needed. PDF supports a NewWindow attribute which PDFsharp cannot currently set.
Get the source for PDFsharp, locate "<</Type/Action/S/Launch/F<</Type/Filespec/F{0}>> >>" in PdfLinkAnnotation.cs and replace it with "<</Type/Action/S/Launch/NewWindow true/F<</Type/Filespec/F{0}>> >>".
Disclaimer: I did not test the proposed change - I hope it is syntactically correct and I hope it does what you want.
Disclaimer 2: This change is needed for PDFsharp 1.50 beta 3. Later versions may have support for that flag.
I have a weird case in a word automation program I'm developing using the Office Interop and C# 3.5
One task of the program is to copy any linked images in the word document to a different location and rewrite the Linked Source of those images to the new location.
Now, in one document, when I check the linked files (using Word 2010), it points an image to a location similar to images\image_file.jpg - So, the image is in a subfolder of the folder where the document is. That's totally correct.
But, when my program runs into that image, the LinkFormat.SourceFullName of that same image gives me a path on our local network, e.g. \\net-storage\customer\001 - customername\data\images\documents\image_file.jpg, without any correlation whatsoever to the actual image link I'm expecting.
What's going wrong here? How do I get the correct image source in my program?
Edit to sw_lasse: I'm sure this is a relative path, because (in other documents) after deleting the image in the relative path and updating the fields in word, the image is not found. So it's definitely a relative path.
Also, the two paths (network and relative) have no correlation to each other. The images on the network use a completely different folder hierarchy, so that's why there's a document subfolder, while it doesn't exist in the relative path.
I know your writing VSTO C# code and this is VBA, but I believe this explains the odd behaviour your seeing.
You cannot use:
myPicPath = Options.DefaultFilePath(wdPicturesPath)
... because if the user has inserted a picture from a different folder, the Default File Path returns that folder rather than the actual setting from the dialog box. Instead, you can use:
With Dialogs(wdDialogToolsOptionsFileLocations)
.Path = "PICTURE-PATH"
.Update
myPicPath = .Setting
If Not Right$(myPicPath, 1) = "\" Then
myPicPath = myPicPath + "\"
End If
MsgBox myPicPath
End With
Similarly, you cannot use:
myDocPath = Options.DefaultFilePath (wdDocumentsPath)
to get the default document path, because this returns the current FileOpen path, not the default documents path!!
Instead, use:
Dim myDocPath As String
myDocPath = Dialogs(wdDialogToolsOptionsFileLocations).Setting
'Add a "\" at the end of the path, unless the setting is already followed by a "\" -
'which it will be if the setting is set to a root folder
If Not Right$(myDocPath, 1) = "\" Then
myDocPath = myDocPath + "\"
End If
MsgBox myDocPath
These days I'm pretty good at converting VBA to C#, if you provide some code that doesn't work I'll convert this VBA to suit.
Ref: How to retrieve Word's default Documents path or Pictures path setting