Relative directory paths in Visual Studio using FSharp - visual-studio

I have a file located in a solution directory like this:
I want to read the contents of one of the .txt files into a string in the FSI:
open System.IO
[<Literal>]
let path = "../Data/Build_Keynote2014.txt"
let buildKeynote = File.ReadAllText(path)
The problem is that it is throwing an exception:
System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\Users\jamie\AppData\Local\Data\Build_Keynote2014.txt'.
Is there a way to reference the file without using the full path?
Thanks

After some research, I found this post
let baseDirectory = __SOURCE_DIRECTORY__
let baseDirectory' = Directory.GetParent(baseDirectory)
let filePath = "Data\Build_Keynote2014.txt"
let fullPath = Path.Combine(baseDirectory'.FullName, filePath)
let buildKeynote = File.ReadAllText(fullPath)
works like a charm. Thanks everyone who submitted.

Related

To check whether a file has been downloaded or not via protractor

Need help to check whether a file has been successfully downloaded or not in a specified folder.
The file name dynamically changes every time a new file is downloaded but the initial part of the file "Pivot_Report" always stays the same
The actual file gets downloaded in the mentioned folder but protractor is not able to find it with just the initial part of the full name
This is the code I'm using (filenamePath is '/Users/Shubh/Documents/')
browser.driver.wait(function() {
        var fileName = filenamePath+"*.csv"
        var filesArray = glob.sync(fileName)
        if (typeof filesArray !== 'undefined' && filesArray.length > 0){
          return filesArray
        }
      }, 10000).then(function(filesArray) {
        var fileWithPath = filesArray[0]
        var temp = fileWithPath.indexOf("Pivot_Report")
        expect(fileWithPath.indexOf("Pivot_Report") >= 0).toBe(true,'Pivot Download is not succesfull')
        if(fs1.existsSync(fileWithPath)){
          fs1.unlinkSync(fileWithPath)
        }
      })
Getting the timeout error
It happens, because your path is incorrect.
I think easiest way is to use path.resolve() :
var path = require("path");
var filenamePath = path.resolve("Users/Shubh/Documents");
and then you will have (notice that you missed a / before *.csv)
var fileName = filenamePath+"/*.csv"

Download zip xml file XAMARIN

I have a command that does the download of the file and saves it in the folder, now to get faster the dowload I want to make it download the zipped file, so the file decreases the size and the dowload will be faster, somebody knows some way for me do that ?
At the moment I use the following command to download
XDocument rss = System.Xml.Linq.XDocument.Load("http://pox.globo.com/rss/g1/mg/sul-de-minas/");
var path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
var filename = Path.Combine(path, "rss.xml");
rss.Save(filename);

Swift XCTest load file

I'm getting confused and haven't been able to find any answers out there.
I have a TestClass in my Test Target. I put a file called test.csv into this target and on disk its stored in the same directory as all the other files.
What 3 lines of code do i need to load the file?
let bundle = NSBundle.mainBundle()
gives me this directory: /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/Library/Xcode/Agents and there doesn't seem to be anything in there.
Oh i figured it out:
NSBundle(forClass: <CLASSNAME>.self).URLForResource("Filename", withExtension: "extension")
Alternatively, returning the path as String:
NSBundle(forClass: <CLASSNAME>.self).pathForResource("Filename", ofType: "extension")

copy from main bundle to application support swift

I have copied files like this from an application before and have copied the code exactly as it appears in another app but for what ever reason when it try to run this particular code it will only create the new directory.
It will not however save the binary file I have saved in supporting files in the main bundle to the new directory. I have done a fair amount of googling and searching stack overflow and have decided I might have to post something here.
let path = NSBundle.mainBundle().resourcePath?.stringByAppendingPathComponent("youtube-dl")
let destination = "~/Library/Application Support/Youtube DLX/"
let destinationPath = destination.stringByStandardizingPath
NSFileManager.defaultManager().createDirectoryAtPath(destinationPath, withIntermediateDirectories: true, attributes: nil, error: nil)
NSFileManager.defaultManager().copyItemAtPath(path!, toPath: destinationPath + "youtube-dl", error: nil)
note that the file that I am trying to copy has no extension so the full name is just "youtube-dl"
The result of
let destinationPath = destination.stringByStandardizingPath
has no trailing slash, so you are trying to copy the file to
"~/Library/Application Support/Youtube DLXyoutube-dl"
You should use
destinationPath.stringByAppendingPathComponent("youtube-dl")
to generate the target path as you already did for the source path.
Generally (as just mentioned in a comment), you should use the error parameter and
check the return value for success, for example
var error : NSError?
if !NSFileManager.defaultManager().copyItemAtPath(..., error: &error) {
println("copy failed: \(error)")
}

Receiving the path of a VS2010 extension?

I've written a small Extension for VS2010 (vsix file), within the vsix is a binary file, which gets called by the extension.
I try to open the file like this:
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = #".\Binaries\TFPT.exe"}
Which works fine if I debug everything. But if I install it, I get a "file not found" error. The Binaries are installed correctly.
So I thought I try to get the complete path to the binaries. But how can I get the path? All Application.Path infos are not pointing to the correct path. Is there a way to get the path of the extension directory?
The path is something like:
C:\Users[username.domain]\AppData\Local\Microsoft\VisualStudio\10.0\Extensions[Company][ExtensionName]\1.0
Any idea without putting it together by hand?
How about retrieving the path from the current executing assembly and use Path.Combine() with your the remaining relative path segment ?
Note: This piece of code comes from a SO answer.
private static string RetrieveAssemblyDirectory()
{
string codeBase = Assembly.GetExecutingAssembly().CodeBase;
var uri = new UriBuilder(codeBase);
string path = Uri.UnescapeDataString(uri.Path);
return Path.GetDirectoryName(path);
}
You can get this path asking for your extension installation path to Visual Studio:
var serviceProvider = [YourPackageObject] as IServiceProvider;
var manager = serviceProvider.GetService(typeof(SVsExtensionManager)) as IVsExtensionManager;
foreach (IInstalledExtension extension in manager.GetInstalledExtensions())
if(extension.Header.Name == [MyExtensionName])
return extension.InstallPath;

Resources