Writing files in VB.Net - Permissions - vb.net

I have a folder that I only want Administrators having permissions to write text files to it.
I need to write text files in a program and insert it into the folder. I am currently using StreamWriter to write the files. It works for me to write the files because I am an administrator and have access to the folder.
I need to have it so all users using the program that may not have access to write to the folder can use the program, and when needed, use the vb.net program to write to the folder but if they try to access the folder they wont have access to write or edit anything in it.
My Idea's where to possibly create a 'Application User' that has access to the folder. This user would be somehow reference when writing the files from the application. Not sure if that is possible.
Dim file As System.IO.StreamWriter
file = My.Computer.FileSystem.OpenTextFileWriter("O:\*****\****\**\****\" + LPlant + LMove + "_" + Format(thisDay, "yyyyMMddhhmmss") + ".dat", True)
file.WriteLine(LPlant + LMove + LMaterial + LQuantity + LLocation + LUnit + LPPOrderNum + LPONum + LPOItem + LBatchNum + LDocumentDT + LBadgeID + LSequenceNum)
file.Close()
Has anyone done anything like this in a vb.net application.
Any help would be appreciated
Thanks

Sorry, that won't work. A program run by user X can only do what user X can do. There is no setuid functionality on Windows.
You can work around this issue by designing your application as a service, either a classic Windows Service or, for example, a locally running web application. That service application would run with a special user account that has the necessary permissions (i.e., write permissions to that directory). Your end users would use the web interface (in case of a web application) or a non-privileged application that can communicate with your privileged service (in case of a Windows service).

Related

ASP / SQL: Filepath as clickable link

I have a database file that looks like the following:
**Name Number1 Number2 File**
Henk 123 456 c:\henk.pdf
Piet 345 789 c:\piet.pdf
When I put this in a SQL Datasource (Visual Studio), the file should be clickable text or image.
Now it's just plain text.
<asp:BoundField DataField="Relatienummer" HeaderText="Relatienummer" SortExpression="Relatienummer" />
<asp:BoundField DataField="nummer" HeaderText="nummer" SortExpression="nummer" />
<asp:BoundField DataField="company" HeaderText="company" SortExpression="company" />
<asp:BoundField DataField="Link" HeaderText="Link" SortExpression="Link" />
What I currently have is:
<asp:HyperLinkField DataNavigateUrlFields="Link" DataNavigateUrlFormatString="Link" DataTextField="Link" HeaderText="Link" />
But this links to link instead of the content of the sql.
Thanks
Remember, code behind can use full file system - like any desktop code.
But if you going to use that file in the asp.net (web side), then you MUST be able to produce a valid URL path name.
So ALWAYS keep in mind the above:
Codebehind - path names are STANDARD windows file path names (and even "\" etc.).
Web pages - markup = valid URL's mapped by the web site.
If you confuse the above two concpets above? you be in for a world of pain by ignoring the above VERY simple idea.
So, you need TWO things to convert those path names to valid WEB links:
First up: The web server "site" needs rights and a "means" to point to a given folder in question.
So, you might have some say SAN or big network storage device - and you toss/place/have/put your files there.
Say like this:
\\BIGSERVER1\PdfFolder\Customers
Now in your code behind? You can use
\\BIGSERVER1\PdfFolder\Customers\Policy.pdf
But for the web server? Well, there is no WAY that you could and would allow just any old server path names on your network to be used by VALID WEB URL's (again, that concept of code behind vs VALID web links MUCH be in your brain thoughts at ALL times).
Ok, so how would we use the above file path on the asp.net + web side? (now we NOT talking about code behind - right?).
Well, in 99% of cases, you thus fire up IIS management, and add what is called a Virtual folder. This thus mapps from the web site to the above folder.
It will look like this:
So in above, the folder UpLoadFiles will and can be mapped say this:
\\BIGSERVER1\PdfFolder\Customers
So, codebehind (your vb/c# code will use this:
\\BIGSERVER1\PdfFolder\Customers\myfile.pdf
But, a web link will be this
http://myCoolWebSite/UpLoadFiles/myfile.pdf
So any hyper link etc. HAS to be a valid URL as per above. And since we ONLY mapped the ONE folder to the web site, then users can't just type in any old email and grab/get/mess with files on that server, or your whole netowrk for that matter. But KEEP in mind your code behind does not have such restrictions - that just plan jane code with plane jane valid full server or simple full valid windows pathnames.
So, in your data table, lets assume the file saved was
myfile.pdf
So, I do suggest that you separate out the valid windows path name from the file.
Ok, so to provide a valid web link, you need this expression:
"~/UpLoadFiles/" + "myfile.pdf"
So this can be a data bind expression, or even part of the SQL query that drives the grid/list or whoever it is displaying.
Last but not least:
Often in your code, you need (want/have to) translate a given valid URL to a correct good old plane jane file path name (after all ANY code behind will only work with those plane jane full valid windows path names.
So, your code can do this:
dim strFileName as string
' assume say you have the database and a row in a "data row"
strFileName = Server.MapPath("~/UpLoadFiles/" & MyDataRow("FileName"))
So above code will of course translate the virtual folder that points
to the server holding the files and return a full valid windows path name
for your code. (the same as the full server pathname I exampled above).
The other approach?
Well, don't setup a virtual folder, and don't allow valid URL paths directly to those files. If you dealing with say customer A files, and customer B files, then allowing valid URL's to those files (that they COULD just type in), then you resort to ONLY allowing and use code behind to get those files.
So, now your hyper link becomes a button. When they click on that button, code behind reads the file and STREAMS (pumps) out the file to the web browser. They will thus get a standard file download when you do this, but NEVER does any actual valid URL exist for them to type in (and thus the files are secure - since then you and your code will fully control who can get what file since no valid URL's that map to that folder question exist. Thus ONLY code behind can continue to use and enjoy plane jane valid windows file names, but the web side of things will not have nor allow valid URL's to the mapped virtual folder, since you did not create one.
Of course if you create a "files folder" as a sub folder in the web site, then yes in most cases valid URL's can be typed in. But if you use server or file folder OUTSIDE of the root web site and folders, then you have to use that virtual folder mapping to create and produce a value URL mapping to the web site.
So, if you want a hyper link? Then want a link button or some such.
iFile='<%# "~/UpLoadFiles/" + Eval("InternalFileName")%>'>
or say this for a hyper link:
src ='<%# "~/UpLoadFiles/" + Eval("InternalFileName")%>'>
So you have to setup a Virtual folder to map out the windows land plane jane folder to that of a valid web based url.
So you could even place that in the SQL query
SELECT ID, [Name], Number1, Number2, File, '~/UpLoadFiles/' + File as FileUrl
FROM tblFiles
So, you now have a valid URL for that hyper link or link button.
You NOT shared how you setup file mapping for the web server, but just keep VERY clear in your mind that code behind STILL WILL ALWAYS use full standard path names. But the web site URL's are not - they are based on the path name of your web site. As noted, you could just place the files in a sub-folder of the root of the web site you have, and then you don't need a virtual folder. But for files outside of the site, or for security reasons, you often have the files and folders for these files NOT in the base web site folders. But adding a virtual (mapped) folder quite much means you can point/expose any valid folder system on the same network that the web server is running on.
I doubt that you allow URL's to drive c:\myfile.pdf.

How to rename a FTP folder using FTPclient from CodeProject in vb.net

i'm currently using FTP Client library from codeproject(https://www.codeproject.com/script/Articles/ListVersions.aspx?aid=11991)
And i cannot figure out how to rename a FTP folder, using their library.
Did some research on google and also could not find a way to do this in vb.net,
the code i'm using to try to achive this task is:
Dim myFtp As New FTPclient(hostname, username, password)
myFtp.CurrentDirectory = "/UDB/"
myFtp.FtpRename("/UDB/" & ListBox1.SelectedItem.ToString & "/", TextBox2.Text)
That shows me an error on the server as:
(not logged in) (::1)>
But in a fact, i am logged in, as i can lunch another task with same credentials and upload and download functions work fine.
Is there a way to achieve this using FtpWebRequest? Thank you.

how to create a list template in SharePoint online using VS 2013

I am going to create some declarative items in SPO using VS. I know how to create it through UI by I want to create programmatically. As I did some research, there are 3 ways.
First is through SP hosted App(add-in) then give manage permission on host web to create some list on its parent site.
Here is the article:http://www.sharepointnadeem.com/2013/12/sharepoint-2013-apps-access-data-in.html
This approach is not good because App web shouldn't apply any change on its host web.
Second is through Sandbox solution. Once I try to create a sandbox solution using SPO site URL, I will receive an error which said connecting to a remote site is not possible through VS. So I have to enter a local SP URL to create a sandbox solution. then I should create list template declaratively and deploy it and publish it to my SPO environment. Here is the article which explain steps:http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2012/01/10/how-to-use-visual-studio-11-to-publish-solutions-to-sharepoint-online.aspx
The problem that I have with this approach is I don't have access to my managed metadata columns which are located in SPO once I am developing list template in my local development machine.
Third approach is creating List or content type or template in App web which is not my case. This has been explained in this article:
sharepoint-journey.com/sharepoint-list-in-sharepoint-hosted-app.html
The problem here is, I want to have that template in my site (host site) not in App web site (sub site)
My question is: I want to provide some list template in my SPO environment by using VS 2013. what is the best approach for doing that? and how can I do that? Please give me an step by step instruction.
Thank you
You definetly want to go the App(Add-in) way. It is possible and in many cases prefereble to set up your lists on the host web and then just use the app web for a user interface, or if you don't want to utilize it at all: Just let the add-in do the heavy lifting in creating lists, columns and content types.
The important thing here is that you specify in your code that you want the lists to be created on the Host Web and not the App Web. For this to work you need to include a permission request in your app. When installed, the app will ask the user installing it if it can have permission to the host web. This is done by setting the relevant scope in your AppManifest.xml file, and then you set it under Permission: [your scope] - [permission level] You can read more about this at this resource: https://msdn.microsoft.com/en-us/library/office/fp142383.aspx
Then something like this code will allow you to create a list on your hostweb. Keep in mind that this list will still be available on the host web even if you remove your app.
oApp.install.addList = function (listName) {
var listCreationInfo = new SP.ListCreationInformation();
listCreationInfo.set_title(listName);
listCreationInfo.set_templateType(SP.ListTemplateType.genericList);
var myNewList = hostcontext.get_web().get_lists().add(listCreationInfo);
var dfd = $.Deferred();
context.load(myNewList);
context.executeQueryAsync(function () {
var listCreated = true;
console.log("[" + listName + "]" + " added to hostweb");
if (listCreated) dfd.resolve();
}, oApp.onFail
);
return dfd.promise();
};
I also wrote about creating certain types of columns on your list at my blog. Feel free to check that out here: http://bayerlein.se/how-to-create-host-web-lists-with-certain-columns-in-your-sharepoint-add-in-the-nice-way/

Opening a file using Sandbox

OK, so I know that under the new SandBox guidelines, opening a file must abide by certain rules (a.k.a. the opening action must be triggered by the user using an NSOpenPanel, given of course the necessary "open" permissions).
However, here's the catch :
In my application, I've got an NSOutlineView with a complete file manager in it (the full tree structure)
The user is supposed to be able to select a file from the outline view and then the app will open it.
How am I supposed to do that, given that the app should be 100% sandbox-compliant? Is there any known workaround? Is it even possible?
Any ideas?
Short answer: You can't do that. In order to show the contents of a folder within your app's UI, you'd first have to get the user to open it either using an open panel or dragging it in from the Finder.
You can do this, as #omz said, your application needs to request permission to access the folder containing all the files/folders that your application is showing. You don't need permission of individual files, but can get an entire directory structure as a single permission, and then store that as a security scoped bookmark so future executions of your application will already have that permission.
You could even at app launch ask the user for permission to access the entire hard drive, or their entire user directory.
You can use this class I wrote to wrap all that up into a single function call, which will then persist the permission so they are only asked on first run. https://github.com/leighmcculloch/AppSandboxFileAccess
Alternatively if you want to do it with NSOpenPanel manually, just take a look at the code in AppSandboxFileAccess as it uses it to get permissions and then persist those permissions.

Get lasting permission to write to a specific directory with the new Sandbox requirements

I need a way to get & keep permission to write to a specific directory in OS X. How can that be done while abiding with the new Sandbox requirements?
The recipe:
Ask the user to select the directory - use a standard open dialog limited to directory selection. Apart from a few special directories (music, pictures etc.) there is no way to gain access apart from asking the user.
Create a security-scoped bookmark using the URL returned by the standard open dialog, just search the Apple docs for "security-scoped bookmark".
Persist that bookmark, either in user preferences or in the Application Support folder for your app.
On application launch, or before you need access, read in the saved bookmark and activate - you'll find out how to do this in the Apple docs as above.