I want to copy a picture to homegroup shared folder.
I have opened cmd.exe through Start Menu > Run > cmd.exe and typed:
copy C:\pic1.png \\SOMECOMP\Users\SOMEONE\Shared
The picture has been copied well.
However, when i try to do the same with C#, like that:
System.Diagnostics.Process.Start(#"cmd.exe", #"/c start copy C:\pic1.png \\SOMECOMP\Users\SOMEONE\Shared");
I get the following message:
Access is denied.
How can i fix this?
P.S. - File.Copy throws the same error. For me, the cmd way looked more promising.
Why on earth would you use Process.Start to copy files when there's File.Copy?
File.Copy(#"C:\pic1.png", #"\\SOMECOMP\Users\SOMEONE\Shared\pic1.png", true);
As far as the access denied message is concerned you will need to ensure that the account you are executing your program under has write permissions to this UNC share which might not be the case with ASP.NET applications or Windows NT services which run under limited privileges accounts.
You probably need to map the directory
& "net" "use" "$mapdrive" "$password" "/USER:$username"
& "copy" $src "$dest"
& "net" "use" $mapdrive "/delete"
But yes, if you can, use the System.IO.File namespace to copy the file.
Related
Today I finally ended creating my app.
To install app I used "setup project" from Visual Studio 2019 (image of project type). App is written in C# .NET 4.7.2 Framework.
So I added some folders and files (image of files and folders here). So I gave my friend *.msi file, he istalled that and something happened - path access denied.
I was surprised because when I installed, everything worked, but on my friend's PC when app is trying to write something in file, it gives result that it cannot open it (just doesn't have permission).
My friend went to properties of those files and he didn't have writing permissions (image here in polish language). In that picture in Polish, zapis
mean "writing", so here is my question: how to make setup project in Visual Studio which gives permission to folders/files to modify them?
In general apps don't have permission to write files to the program folders without elevated permissions. Most apps should write data to the users %appdata% folder you can access that path in c# using:
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
Every work on files and folders must be in Users\user folders, in Documents\folder or in the registry. You shouldn't give permissions to the user to modify files on the system or Program Files.
Although windows installer is able to set file permissions, you should not write anything in Program Files. And you should not grant write permissions there due to security considerations and best practices. So, you need to inspect your code and change all places where you are trying to write in a Program Files (or in the folder where your application is placed) to write somewhere else. For example, in the %temp% folder, in the Program Data or in specific folder inside a user's Documents folder or even ask user to specify write location explicitly.
I have a batch file which disables and enables some driver using windows devcon.
when I run this batch file by double-clicking on it it works fine.
However, I tried to run it from a C# program that I wrote using this line:
System.Diagnostics.Process.Start("C:/*path to file*/file.bat");
it runs fine until it tries to open the devcon.exe and I get the following message:
after that it continues to run smoothly.
any ideas why it doesn't work from the C# program?
p.s
I can't post the batch code due to IP issues...
The problem is - as often - the "working directory". When you double-click something in the Explorer, the working directory is set to the current folder, so from the batch file's point of view it's current directory is its own directory.
When you execute a C# application, usually the working directory is the directory of the application's exe file, but not necessarily (for example if the application is run using a link, you can specify a different working directory). That's why, to find the application EXE file's directory it is not save to use GetCurrentDirectory.
So what happens is that the application runs the batch file, but with the application's directory, not the batch file's directory, as working directory. An alternative to an explicit cd within the batch file would be to specify the working directory when calling Process.Start.
ok, after a little bit of research I found this simple solution:
simply changing to the directory of the devcon.exe (using cd command) at the beginning of the batch code, i.e:
cd "C:/*path to the directory of devcon.exe*"
#rest of the code
as you know, C:\Program Files is a restricted folder and Only administrators can modify the contents of this folder.
my c# application has ability to update him self. so i need to download some files programmatically from server and replace them with old files. those files are in Program Files. what can i do about it?
You're pretty stuck, if you need to have your application run anywhere that requires Administrator privilege to update. You are forced to elevate your process's privilege to administrator level to perform your update.
A solution to this problem is to install your application somewhere else--someplace that does not require administrator privilege.
You are going to need to spawn a helper application, that will escalate to get the appropriate permissions, copy the new binary where it needs to go, and exits. Perhaps you can use copy.exe in conjunction with a Process.Start that knows to escalate. This SO article seems to indicate you can do it without too much trouble. However, there is no way around the admin rights if your program lives in C:\Program Files
I'm writing an application which starts through command prompt mysql.exe and mysqldump.exe.
The idea is to make a backup and then upload it to mysql with different database name.
The problem is that I didn't think what would happen if these mysql.exe and mysqldump.exe are not registered in the cmd and you have to type the path to them manually.
And my application breaks.
So I'm wondering is there any automatic way to find the path to these two files to run them correctly.
The other solution that comes to my mind is to let the user browse his files and select these executables.
To manage this path you can use an environment variable and get it from your application.
string sPath = System.Environment.GetEnvironmentVariable("MYSQL_DIR");
Other solution is copy both executables to your application path and access them with a relative path.
string sPath = "./";
Hope it helps.
In my project I have to create some files and directories into my app folder which is into program files. But in vista it is giving me error that I dont have access to create file.
What should I do now for giving access ? Also it not let me access the registry !!
The program folder is not the place to store application data. There is an %APPDATA% folder for that - you are supposed to store your data there.
Use System.Environment.SpecialFolder and System.Environment.GetFolderPath to obtain the path leading to the correct directory.
Also, you need to differentiate between just creating a folder and putting some files in there (for example during installation) or writing to the program folder at runtime, while typically running under a limited account.
The reason for this difference is simply that installation routines and setups run with elevated privileges under Vista / Windows 7, thus those are allowed to create folders and files there. Still, those files are not supposed to be written to at runtime of your application.
So, what is it you want to do? Write data at runtime, or put some files (i.e. dependencies) in your application folder at a single time? If it's the first, comply with the rules and use the %APPDATA% folder. If it's the second, create an installer / setup routine.
Vista and Win 7 have the Program Files folder locked down so you can't write to it with a basic user account. If you need to create folders there, then you should do it in the installer. Otherwise you can use the user's Application Data folder in their profile.
The only other way is to modify the permissions on the installation folder at install time.
Can you turn the UAC off? Or Login as administrator? The chances are that you are creating the folder in the wrong place.