I want get the folder names from server.MapPath in ASP.NET MVC 3 application.
In this action, I have to check (if there exist more folders in a given folder name) if a .jpg file is in that folder and if so, return that folder.
string path = Server.MapPath("Content/");
DirectoryInfo dInfo = new DirectoryInfo(path);
DirectoryInfo[] subdirs = dInfo.GetDirectories();
if (Directory.Exists(path))
{
ArrayList ar = new ArrayList();
// This path is a directory
ar.Add(path);
//ProcessDirectory(path);
}
I'm not sure I've understand the qestion correctly, but I think you want something like
string path = Server.MapPath(YOURPATH);
List<string> files = Directory.GetFiles(path, "*.jpg", SearchOption.AllDirectories);
or something like
string path = Server.MapPath(YOURPATH);
List<string> picFolders = new List<string>();
if(Directory.GetFiles(path, "*.jpg").Length > 0)
picFolders.Add(path)
foreach(string dir in Directory.GetDirectories(path, "*", SearchOption.AllDirectories))
{
if(Directory.GetFiles(dir, "*.jpg").Length > 0)
picFolders.Add(dir)
}
Related
I'm using VS2015, MVC 5 design model. Creating a link that says "View" to open a PDF file in a new browser tab. It works fine, but the document sub-directory is hard-coded in the controller. I need to pass the document sub-directory + filename to the controller. The document sub-directory is the same as the Model.id . I'm having a difficult time converting the Model.id to a string and concatenating with the filename.
The following code in the view works fine with the hard-coded sub-directory in the controller
<td>#Html.ActionLink("View", "ViewAttachedDoc", "Documents", new { filename = item.filename}, new { target = "_blank" })</td>
But this code does not work
<td>#Html.ActionLink("View", "ViewAttachedDoc", "Documents", new { filename = Convert.ToString(Model.id) + "\" + item.filename }, new { target = "_blank" })</td>
The controller action is:
public FileResult ViewAttachedDoc(string filename)
{
string DocPath = ConfigurationManager.AppSettings["DocPath"];
string path = Path.Combine(DocPath, filename);
return File(path, "application/pdf");
}
TIA,
Tracy
The issue is likely from trying to pass a backslash through the URL. This link of a similar question has that same problem and their solution was to use HttpUtility.UrlEncode(value); and HttpUtility.UrlDecode(value);. Otherwise if that still doesn't work, what error are you getting?
P.S. C# automatically converts / -> \ for retrieving files.
I have a class that makes a copy of a google doc template file to then merge placeholders with data. The problem is that it's creating the copy in the same folder as the original template file and I can't find a way to copy it to another folder.
Say I have a structure like so in google drive.
Main
TemplateFiles
MainTemplateDoc
Contracts
CopyOfMainTemplateDocAfterBeingMerged
As you can see, the CopyOfMainTemplateDocAfterBeingMerged doc wouldn't be located in the TemplateFiles folder where the original template was, but copied to the Contracts folder.
Is it possible to use the google drive v2 service to move the file after creating the copy? I'm using the .net Google.Apis.Drive.v2 nuget package. Here's the code that I have so far to create the copy.
private string CopyDocument(string documentId, string title)
{
var newFile = new File { Title = title };
var documentCopyFile = driveService.Files.Copy(newFile, documentId).Execute();
return documentCopyFile.Id;
}
You are using Drive API v2.
You want to copy a file to the folder of Contracts.
You have already been able to use Drive API.
If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.
In this answer, I would like to propose the following modification.
From:
var newFile = new File { Title = title };
To:
var newFile = new File {
Title = title,
Parents = new List<ParentReference> {new ParentReference {Id = parentId}}
};
or
var newFile = new File() {
Title = title
};
newFile.Parents = new List<ParentReference>() {new ParentReference() {Id = parentId}};
or
var newFile = new File();
newFile.Title = title;
newFile.Parents = new List<ParentReference>() {new ParentReference() {Id = parentId}};
parentId is the folder ID of the folder Contracts.
Note:
If you want to use Drive API v3, please use new List<string> {folderId} instead of new List<ParentReference> {new ParentReference {Id = folderId}}.
References:
Files: copy
Files: insert
If I misunderstood your question and this was not the direction you want, I apologize.
I am making a small project there is a one module, which is KYC update and In this module i am save AadharCard,PAN Card and Other documents in Folder.So I am using java IO and try to create directory which name is Document and i want to create a sub-directory with user name and for create unique a also append username,id concat.I write a code but the sub-directory didn't crate.
My code is
String uploadPath = context.getRealPath("") + "assets" + File.separator + "Document" + File.separator
+ userPojo.getFirstName();
File file = new File(uploadPath);
try {
if (!file.exists()) {
file.mkdirs();
logger.debug("Make Dir");
}
and I also try this
File file = new File(dir, userPojo.getUserName());
if (!file.exists()) {
file.mkdirs();
logger.debug("Make Dir");
}
I search and i solve my problem.So i want to give solution if anyone face this type of problem
File file = new File(uploadPath);
File file2 = new File(uploadPath, dataPojo.getFirstName()));
if (!file.exists()) {
file.mkdirs();
logger.debug(file.getAbsolutePath());
}
if (!file2.exists()) {
file2.mkdirs();
logger.debug(file.getAbsolutePath());
}
I want to create a list of all the .xlsx files on my computer and the number of rows in them.
What is the best way to get the # of rows in each .xlsx file? I'm on Mac OSX Yosemite (if it's relevant). The answer can be in any language.
You can do it with PHP and Spout (https://github.com/box/spout).
The code would look something like this:
use Box\Spout\Reader\ReaderFactory;
use Box\Spout\Common\Type;
// This is the root directory you want to list the XLSX files of
// CHANGE IT !!
$rootPath = '/path/to/root/directory';
// this will read the XLSX file at the given path and return the number of rows for the file
function getNumRows($xlsxPath) {
$numRows = 0;
$reader = ReaderFactory::create(Type::XLSX);
$reader->open($xlsxPath);
foreach ($reader->getSheetIterator() as $sheet) {
foreach ($sheet->getRowIterator() as $row) {
$numRows++;
}
}
$reader->close();
return $numRows;
}
// This will recursively go through the root folder and all its subfolders
$directoryIterator = new \RecursiveDirectoryIterator($rootPath, RecursiveDirectoryIterator::SKIP_DOTS);
$recursiveIterator = new \RecursiveIteratorIterator($directoryIterator, RecursiveIteratorIterator::SELF_FIRST);
foreach ($recursiveIterator as $fileInfo) {
if (strtolower($fileInfo->getExtension()) === 'xlsx') {
$xlsxPath = $fileInfo->getPathname();
$numRows = getNumRows($xlsxPath);
// store the number of rows for the file somewhere
}
}
I'm trying to get a specific folder in outlook with c#. Someone else had the same issue here Using Outlook API to get to a specific folder but when using the Folders collection I'm not sure how to get through the folders collection. I mean, I've looked at the type of object that the Folders collection returns and it looks like it's a Folders object. But when I try to use that in a loop it gives me an invalid cast exception. I also hoped I could use the GetFolderFromID method to give it the string name of the folder but that doesn't want to work either... but I also can't find an example of how to use it so I'm not sure I'm coding it correctly. Here's an example of what I tried. Anyone know how to get the Processed folder which is on the same level as the Inbox folder? Thanks.
MAPIFolder oProcessed = null;
foreach (var folder in oNS.Folders)
if (folder.ToString() == "Processed")
{
oProcessed = (MAPIFolder)folder;
}
if (oProcessed == null)
throw new Exception("Missing processed folder.");
you need to get hold of the root level mailbox folder
Outlook.MAPIFolder rootFolder= Application.Session.DefaultStore.GetRootFolder();
Then loop through the rootFolder folders collection check in the names
Outlook.MAPIFolder processedFolder = null;
foreach (Outlook.MAPIFolder folder in rootFolder.Folders)
{
if (folder.Name == "Processed")
{
processedFolder = folder;
break;
}
}
Check out http://msdn.microsoft.com/en-us/library/bb176810.aspx to get you head round the API.
Marcus
Outlook.MAPIFolder rootFolder = Application.Session.DefaultStore.GetRootFolder();
var processedFolder = rootFolder.Folders.Cast<Outlook.MAPIFolder>().Where(r => r.Name == "Processed").FirstOrDefault();
This is an inept translation from VBA, but may offer some ideas, seeing you have no answers as yet. In VBA, it is best to get the parent folder of Inbox and to look in that for folders at the same level.
Microsoft.Office.Interop.Outlook._Folders oFolders;
Microsoft.Office.Interop.Outlook.MAPIFolder oPublicFolder =
olNS.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox).Parent;
//Folders at Inbox level
oFolders = oPublicFolder.Folders;
foreach (Microsoft.Office.Interop.Outlook.MAPIFolder Folder in oFolders)
{
string foldername = Folder.Name;
if (foldername == "Test")
Console.Write(Folder.Name);
}
If you have the folder path as a string, you can use this function:
private MAPIFolder GetFolder(string folderPath, Folders folders)
{
string dir = folderPath.Substring(0, folderPath.Substring(4).IndexOf("\\") + 4);
try
{
foreach (MAPIFolder mf in folders)
{
if (!(mf.FullFolderPath.StartsWith(dir))) continue;
if (mf.FullFolderPath == folderPath) return mf;
else
{
MAPIFolder temp = GetFolder(folderPath, mf.Folders);
if (temp != null)
return temp;
}
}
return null;
}
catch { return null; }
}