Upload files to a specific folder in dropbox - c#

I was using this example to upload files to dropbox:
https://github.com/geersch/DropboxRESTApi/blob/master/src/part-5/README.md
Which is:
public FileSystemInfo UploadFile(string root, string path, string file)
{
//...
}
The method has 3 parameters:
root: The root relative to which the path is specified. Valid values
are sandbox and dropbox.
path: The path to the file you want to
upload.
file: Absolute path to the local file you want to upload
Call this method as follows:
var file = api.UploadFile("dropbox", "photo.jpg", #"C:\Pictures\photo.jpg");
Dropbox API says exactly the same as the example:
https://www.dropbox.com/developers/core/docs#files-POST
So no mentioning of "parent folder"..
How can I upload files under a specific folder instead of the root?

cant you just do
var file = api.UploadFile("dropbox", #"myfolder\photo.jpg", #"C:\Pictures\photo.jpg");

I have the same issue.
I use this method for combining paths as the target directory
public static string CombinePath(string folder, string filename) => folder == "/" ? $"/{filename}" : $"{folder}/{filename}";
If you need to upload your file into the root directory the only parameter is / to identify the root.

Related

Convert Relative Path to Absolute Path

I am trying to open a Help.txt file in windows Forms using a linkLabel. However unable to convert from absolute to relative path.
First, I try to get the absolute path of the exe file. Which is successful.
Second, get only directory of the exe file. Which is successful.
Third, I am trying to combine the directory with the relative path of the Help.txt file. Which is unsuccessful.
Exe file lives in -> \Project\bin\Debug folder, However the Help.txt file lives in \Project\Help folder. This is my code:-
string exeFile = (new System.Uri(Assembly.GetEntryAssembly().CodeBase)).AbsolutePath;
string Dir = Uri.UnescapeDataString(Path.GetDirectoryName(exeFile));
string path = Path.Combine(Dir, #"..\..\Help\Help.txt");
System.Diagnostics.Process.Start(path);
The result of my path is -> \Project\bin\Debug....\Help\Help.txt
You need to use Path.GetFullPath() to have the upper directory "../../" taken into account, see below :
string exeFile = new System.Uri(Assembly.GetEntryAssembly().CodeBase).AbsolutePath;
string Dir = Path.GetDirectoryName(exeFile);
string path = Path.GetFullPath(Path.Combine(Dir, #"..\..\Help\Help.txt"));
System.Diagnostics.Process.Start(path);
Per the MSDN of GetFullPath : Returns the absolute path for the specified path string.
Whereas Path.Combine Combines strings into a path.

How to get a file full path in c# with Path.GetFullPath

I am trying to get the full path a file by its name only.
I have tried to use :
string fullPath = Path.GetFullPath("excelTest");
but it returns me an incorrect path (something with my project path).
I have read somewhere here a comment which says to do the following:
var dir = Environment.SpecialFolder.ProgramFilesX86;
var path = Path.Combine(dir.ToString(), "excelTest.csv");
but I do not know where the file is saved , therefore I do not know its environment.
can someone help me how to get the full path of a file only by its name?
The first snippet (with Path.GetFullPath) does exactly what you want. It returns something with your project path because the program EXE file is located in the project\Bin\Debug path, which is therefore the "current directory".
If you want to search for a file on a drive, you can use Directory.GetFiles, which will recursively search for a file in a directory given a name pattern.
This returns all xml-files recursively :
var allFiles = Directory.GetFiles(path, "*.xml", SearchOption.AllDirectories);
http://msdn.microsoft.com/en-us/library/ms143316%28v=vs.100%29.aspx
http://msdn.microsoft.com/en-us/library/ms143448.aspx#Y252
https://stackoverflow.com/a/9830162/2196124
I guess you're trying to find file (like in windows search), right ?
I'd look into this question - you will find all files that has that string in their filename, and from there you can return full filepath.
var fileList = new DirectoryInfo(#"c:\").GetFiles("*excelTest*", SearchOption.AllDirectories);
And then just use foreach to do you manipulations, e.g.
foreach(string file in fileList)
{
// MessageBox.Show(file);
}
What you're looking for is Directory.GetFiles(), you can read up on it here. The gist of it is, you'll pass in the file path and the file name, and you'll get a string array back. In this instance, you can assume top level with C:\. It should be noted, that if nothing is found, the string array will be empty.
You have passed a relative file name to Path.GetFullPath. Microsoft documentation states:
If path is a relative path, GetFullPath returns a fully qualified path that can be based on the current drive and current directory. The current drive and current directory can change at any time as an application executes. As a result, the path returned by this overload cannot be determined in advance.
You cannot get the same full path name from a relative path unless your current directory is the same each time you invoke the function.

Where are files saved in MVC4 2010 project where no path is declared?

I've created a file with the filepath of simply "test.txt" using File.Create method.
I've managed to email this file to myself using an email class and the filepath of "test.txt".
I want to find the actual file in my project folder, but can't see to find it even with a search for test.txt.
Is it a temp file, and where would it be saved to?
I'm creating the file with the following code:
public void createCSVFile()
{
string fileLocation = "test";
mFilePath = fileLocation+".txt";
if (!File.Exists(mFilePath))
File.Create(mFilePath).Close();
}
I think the following will help you with mapping your files in a specific location to help you further:
Server.MapPath specifies the relative or virtual path to map to a physical directory.
Server.MapPath("."), Server.MapPath(null), Server.MapPath("") will return the physical directory of the file being executed
Server.MapPath("..") will return the parent directory
Server.MapPath("~") will return the physical path to the root of the application
To give you an example if you have a website that is stored on disk at C:\Website the mapPath will return the following :
Server.MapPath("..") will return : "C:\Website"
Server.Mappath(".") will return "C:\Website{controllerName}"
Server.MapPath("~") will return : "C:\Website"

File.Delete isn't working to delete image from sub folder

In our ASP.NET program a user can upload an image to a folder. The location of the image (including the name of the upload folder which is in the root directory) is stored as a variable called "path", ie. "Uploads/fileName.jpg".
To remove the image:
if (File.Exists("~/" + path))
{
File.Delete("~/" + path);
}
However, it fails to run because it can't verify that the file exists. Through some testing we noticed it's looking for "path" in the "system32" directory. Why would this be?
You need to use Server.Map path to ensure that the Tilde is resolved correctly.
MSDN Article is here -> http://msdn.microsoft.com/en-us/library/ms524632(v=vs.90).aspx
Your code would become
var fixedPath = Server.MapPath("~/" + path);
if (File.Exists(fixedPath))
{
File.Delete(fixedPath);
}
The File class is not aware of the IIS directory mapping, so it won't understand ~ correctly. You have to first use a method to map the app relative path to a local path with Server.MapPath

Get file paths from a folder

I am looking to get all the file paths from my images folder.
The file path in relation to my root is image/picture.jpg
I have seen the example of
foreach (var path in Directory.GetFiles(#"\image\"))
But this looks for a direct path, I am unsure as to how to use this code.
Can anyone suggest what I should be using?
Use Server.MapPath as so:
foreach (var path in Directory.GetFiles(Server.MapPath( "~/image")))
{
}
Server.MapPath returns the physical file path that corresponds to the specified virtual path on the Web server.

Categories

Resources